1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
19 using Castle
.ActiveRecord
.Framework
;
21 using NHibernate
.Criterion
;
23 [ActiveRecord("BlogTable")]
24 public class Blog
: ActiveRecordBase
28 private String _author
;
30 private IList _publishedposts
;
31 private IList _unpublishedposts
;
32 private IList _recentposts
;
33 private bool onSaveCalled
, onUpdateCalled
, onDeleteCalled
, onLoadCalled
;
44 [PrimaryKey(PrimaryKeyType
.Native
)]
55 set { _name = value; }
61 get { return _author; }
62 set { _author = value; }
65 [HasMany(typeof(Post
), Table
="Posts", ColumnKey
="blogid")]
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);
159 /// Lifecycle method invoked during Save of the entity
161 protected override void OnSave()
167 /// Lifecycle method invoked during Update of the entity
169 protected override void OnUpdate()
171 onUpdateCalled
= true;
175 /// Lifecycle method invoked during Delete of the entity
177 protected override void OnDelete()
179 onDeleteCalled
= true;
183 /// Lifecycle method invoked during Load of the entity
185 protected override void OnLoad(object id
)
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
215 ActiveRecordMediator
.Execute(typeof(Blog
), new NHibernateDelegate(GrabSession
), this);
219 private static object GrabSession(ISession session
, object instance
)
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
);
237 internal static ISessionFactoryHolder Holder
239 get { return ActiveRecordMediator.GetSessionFactoryHolder(); }