Added RedirectUsingNamedRoute
[castle.git] / Samples / MindDump / Castle.Applications.MindDump / Dao / PostDao.cs
blob2b6857661ee0c4c5088f6a833096e88883457219
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Applications.MindDump.Dao
17 using System.Collections;
19 using Castle.Applications.MindDump.Model;
20 using Castle.Facilities.NHibernateIntegration;
22 using NHibernate;
24 public class PostDao
26 private readonly ISessionManager sessionManager;
28 public PostDao(ISessionManager sessionManager)
30 this.sessionManager = sessionManager;
33 public Post Create(Post post)
35 using(ISession session = sessionManager.OpenSession())
37 session.Save(post);
39 return post;
43 public void Update(Post post)
45 using(ISession session = sessionManager.OpenSession())
47 session.Update(post);
51 /// <summary>
52 /// Usually will be invoked only be the
53 /// test cases
54 /// </summary>
55 public void DeleteAll()
57 using(ISession session = sessionManager.OpenSession())
59 session.Delete("from Post");
63 public IList Find()
65 using(ISession session = sessionManager.OpenSession())
67 return session.Find("from Post order by id desc");
71 public IList Find(Blog blog)
73 using(ISession session = sessionManager.OpenSession())
75 IList list = session.Find(
76 "from Post as a where a.Blog.Id=:name", blog.Id, NHibernateUtil.Int64);
78 return list;
82 public Post Find(long id)
84 using(ISession session = sessionManager.OpenSession())
86 IList list = session.Find(
87 "from Post as a where a.id=:name order by id desc", id, NHibernateUtil.Int64);
89 if (list.Count == 1)
91 return list[0] as Post;
93 else
95 return null;
100 public IList FindLatest(int howMany)
102 using(ISession session = sessionManager.OpenSession())
104 IList list = session.Find("from Post order by id desc");
106 if (list.Count > howMany)
108 list = ListUtil.Limit(howMany, list);
111 return list;