Added RedirectUsingNamedRoute
[castle.git] / ActiveRecord / Castle.ActiveRecord.Tests / Model / Blog.cs
blob7fd97c61166a17ccf8e9a9247686d3d6d5db4881
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.ActiveRecord.Tests.Model
17 using System;
18 using System.Collections;
19 using Castle.ActiveRecord.Framework;
20 using NHibernate;
21 using NHibernate.Expressions;
23 [ActiveRecord("BlogTable")]
24 public class Blog : ActiveRecordBase
26 private int _id;
27 private String _name;
28 private String _author;
29 private IList _posts;
30 private IList _publishedposts;
31 private IList _unpublishedposts;
32 private IList _recentposts;
33 private bool onSaveCalled, onUpdateCalled, onDeleteCalled, onLoadCalled;
35 public Blog()
39 public Blog(int _id)
41 this._id = _id;
44 [PrimaryKey(PrimaryKeyType.Native)]
45 public int Id
47 get { return _id; }
48 set { _id = value; }
51 [Property]
52 public String Name
54 get { return _name; }
55 set { _name = value; }
58 [Property]
59 public String Author
61 get { return _author; }
62 set { _author = value; }
65 [HasMany(typeof(Post), Table="Posts", ColumnKey="blogid")]
66 public IList Posts
68 get { return _posts; }
69 set { _posts = value; }
72 [HasMany(typeof(Post), Table="Posts", ColumnKey="blogid", Where="published = 1")]
73 public IList PublishedPosts
75 get { return _publishedposts; }
76 set { _publishedposts = value; }
79 [HasMany(typeof(Post), Table="Posts", ColumnKey="blogid", Where="published = 0")]
80 public IList UnPublishedPosts
82 get { return _unpublishedposts; }
83 set { _unpublishedposts = value; }
86 [HasMany(typeof(Post), Table="Posts", ColumnKey="blogid", OrderBy="created desc")]
87 public IList RecentPosts
89 get { return _recentposts; }
90 set { _recentposts = value; }
93 public static void DeleteAll()
95 ActiveRecordMediator.DeleteAll(typeof(Blog));
98 public static void DeleteAll(string where)
100 ActiveRecordMediator.DeleteAll(typeof(Blog), where);
103 public static Blog[] FindAll()
105 return (Blog[]) ActiveRecordMediator.FindAll(typeof(Blog));
108 public static Blog Find(int id)
110 return (Blog) ActiveRecordMediator.FindByPrimaryKey(typeof(Blog), id);
113 public static int FetchCount()
115 return Count(typeof(Blog));
118 public static int FetchCount(string filter, params object[] args)
120 return Count(typeof(Blog), filter, args);
123 public static int FetchCount(params ICriterion[] criterias)
125 return Count(typeof(Blog), criterias);
128 public static bool Exists()
130 return Exists(typeof(Blog));
133 public static bool Exists(string filter, params object[] args)
135 return Exists(typeof(Blog), filter, args);
138 public static bool Exists(int id)
140 return Exists(typeof(Blog), id);
143 public static bool Exists(params ICriterion[] criteria)
145 return Exists(typeof(Blog), criteria);
148 public static Blog[] FindByProperty(String property, object value)
150 return (Blog[]) FindAllByProperty(typeof(Blog), property, value);
153 public static Blog[] FindByProperty(String property, String orderByColumn, object value)
155 return (Blog[]) FindAllByProperty(typeof(Blog), orderByColumn, property, value);
158 /// <summary>
159 /// Lifecycle method invoked during Save of the entity
160 /// </summary>
161 protected override void OnSave()
163 onSaveCalled = true;
166 /// <summary>
167 /// Lifecycle method invoked during Update of the entity
168 /// </summary>
169 protected override void OnUpdate()
171 onUpdateCalled = true;
174 /// <summary>
175 /// Lifecycle method invoked during Delete of the entity
176 /// </summary>
177 protected override void OnDelete()
179 onDeleteCalled = true;
182 /// <summary>
183 /// Lifecycle method invoked during Load of the entity
184 /// </summary>
185 protected override void OnLoad(object id)
187 onLoadCalled = true;
190 public bool OnSaveCalled
192 get { return onSaveCalled; }
195 public bool OnUpdateCalled
197 get { return onUpdateCalled; }
200 public bool OnDeleteCalled
202 get { return onDeleteCalled; }
205 public bool OnLoadCalled
207 get { return onLoadCalled; }
210 public ISession CurrentSession
214 return (ISession)
215 ActiveRecordMediator.Execute(typeof(Blog), new NHibernateDelegate(GrabSession), this);
219 private static object GrabSession(ISession session, object instance)
221 return session;
224 public void CustomAction()
226 ActiveRecordMediator.Execute(typeof(Blog), new NHibernateDelegate(MyCustomMethod), this);
229 private static object MyCustomMethod(ISession session, object blogInstance)
231 session.Delete(blogInstance);
232 session.Flush();
234 return null;
237 internal static ISessionFactoryHolder Holder
239 get { return ActiveRecordMediator.GetSessionFactoryHolder(); }