Fixing Brail Templates for VS Net wizards.
[castle.git] / ActiveRecord / Castle.ActiveRecord / Framework / ActiveRecordMediator.Generic.cs
blob78772dbb5f33d9a67987ceff65fdf8b863672e4b
1 // Copyright 2004-2007 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 #if DOTNET2
17 namespace Castle.ActiveRecord
19 using Framework;
20 using NHibernate.Expression;
22 /// <summary>
23 /// Allow programmers to use the
24 /// ActiveRecord functionality without extending <see cref="ActiveRecordBase"/>
25 /// </summary>
26 public class ActiveRecordMediator<T> : ActiveRecordMediator where T : class
28 /// <summary>
29 /// Invokes the specified delegate passing a valid
30 /// NHibernate session. Used for custom NHibernate queries.
31 /// </summary>
32 /// <param name="call">The delegate instance</param>
33 /// <param name="instance">The ActiveRecord instance</param>
34 /// <returns>Whatever is returned by the delegate invocation</returns>
35 public static object Execute(NHibernateDelegate call, T instance)
37 return ActiveRecordMediator.Execute(typeof(T), call, instance);
40 /// <summary>
41 /// Finds an object instance by its primary key.
42 /// </summary>
43 /// <param name="id">ID value</param>
44 /// <param name="throwOnNotFound"><c>true</c> if you want an exception to be thrown
45 /// if the object is not found</param>
46 /// <exception cref="NHibernate.ObjectNotFoundException">if <c>throwOnNotFound</c> is set to
47 /// <c>true</c> and the row is not found</exception>
48 public static T FindByPrimaryKey(object id, bool throwOnNotFound)
50 return (T) ActiveRecordMediator.FindByPrimaryKey(typeof(T), id, throwOnNotFound);
53 /// <summary>
54 /// Finds an object instance by its primary key.
55 /// </summary>
56 /// <param name="id">ID value</param>
57 public static T FindByPrimaryKey(object id)
59 return (T) ActiveRecordMediator.FindByPrimaryKey(typeof(T), id, true);
62 /// <summary>
63 /// Searches and returns the first row.
64 /// </summary>
65 /// <param name="orders">The sort order - used to determine which record is the first one</param>
66 /// <param name="criterias">The criteria expression</param>
67 /// <returns>A <c>targetType</c> instance or <c>null</c></returns>
68 public static T FindFirst(Order[] orders, params ICriterion[] criterias)
70 return (T) ActiveRecordMediator.FindFirst(typeof(T), orders, criterias);
73 /// <summary>
74 /// Searches and returns the first row.
75 /// </summary>
76 /// <param name="criterias">The criteria expression</param>
77 /// <returns>A <c>targetType</c> instance or <c>null</c></returns>
78 public static T FindFirst(params ICriterion[] criterias)
80 return (T) ActiveRecordMediator.FindFirst(typeof(T), criterias);
83 /// <summary>
84 /// Searches and returns the first row.
85 /// </summary>
86 /// <param name="detachedCriteria">The criteria.</param>
87 /// <param name="orders">The sort order - used to determine which record is the first one.</param>
88 /// <returns>A <c>targetType</c> instance or <c>null.</c></returns>
89 public static T FindFirst(DetachedCriteria detachedCriteria, params Order[] orders)
91 return (T) ActiveRecordMediator.FindFirst(typeof(T), detachedCriteria, orders);
94 /// <summary>
95 /// Searches and returns the first row.
96 /// </summary>
97 /// <param name="criteria">The criteria expression</param>
98 /// <returns>A <c>targetType</c> instance or <c>null</c></returns>
99 public static T FindFirst(DetachedCriteria criteria)
101 return (T)ActiveRecordMediator.FindFirst(typeof(T),criteria);
104 /// <summary>
105 /// Searches and returns the first row.
106 /// </summary>
107 /// <param name="criterias">The criterias.</param>
108 /// <returns>A instance the targetType or <c>null</c></returns>
109 public static T FindOne(params ICriterion[] criterias)
111 return (T) ActiveRecordMediator.FindOne(typeof(T), criterias);
114 /// <summary>
115 /// Searches and returns a row. If more than one is found,
116 /// throws <see cref="ActiveRecordException"/>
117 /// </summary>
118 /// <param name="criteria">The criteria</param>
119 /// <returns>A <c>targetType</c> instance or <c>null</c></returns>
120 public static T FindOne(DetachedCriteria criteria)
122 return (T)ActiveRecordMediator.FindOne(typeof(T), criteria);
125 /// <summary>
126 /// Returns all instances found for the specified type.
127 /// </summary>
128 /// <returns></returns>
129 public static T[] FindAll()
131 return (T[]) ActiveRecordMediator.FindAll(typeof(T));
134 /// <summary>
135 /// Returns all instances found for the specified type
136 /// using sort orders and criterias.
137 /// </summary>
138 /// <param name="orders"></param>
139 /// <param name="criterias"></param>
140 /// <returns></returns>
141 public static T[] FindAll(Order[] orders, params ICriterion[] criterias)
143 return (T[])ActiveRecordMediator.FindAll(typeof(T), orders, criterias);
146 /// <summary>
147 /// Returns all instances found for the specified type
148 /// using criterias.
149 /// </summary>
150 /// <param name="criterias"></param>
151 /// <returns></returns>
152 public static T[] FindAll(params ICriterion[] criterias)
154 return (T[])ActiveRecordMediator.FindAll(typeof(T), criterias);
157 /// <summary>
158 /// Returns all instances found for the specified type according to the criteria
159 /// </summary>
160 public static T[] FindAll(DetachedCriteria detachedCriteria, params Order[] orders)
162 return (T[])ActiveRecordMediator.FindAll(typeof(T), detachedCriteria, orders);
165 /// <summary>
166 /// Returns a portion of the query results (sliced)
167 /// </summary>
168 public static T[] SlicedFindAll(int firstResult, int maxResults, Order[] orders, params ICriterion[] criterias)
170 return (T[]) ActiveRecordMediator.SlicedFindAll(typeof(T), firstResult, maxResults, orders, criterias);
173 /// <summary>
174 /// Returns a portion of the query results (sliced)
175 /// </summary>
176 public static T[] SlicedFindAll(int firstResult, int maxResults, params ICriterion[] criterias)
178 return (T[]) ActiveRecordMediator.SlicedFindAll(typeof(T), firstResult, maxResults, null, criterias);
181 /// <summary>
182 /// Returns a portion of the query results (sliced)
183 /// </summary>
184 public static T[] SlicedFindAll(int firstResult, int maxResults,
185 DetachedCriteria criteria)
187 return (T[])ActiveRecordMediator.SlicedFindAll(typeof(T), firstResult, maxResults, criteria);
190 /// <summary>
191 /// Deletes all entities of <typeparamref name="T"/>.
192 /// </summary>
193 public static void DeleteAll()
195 ActiveRecordMediator.DeleteAll(typeof(T));
198 /// <summary>
199 /// Deletes all entities of <typeparamref name="T"/> that match the HQL where clause.
200 /// </summary>
201 public static void DeleteAll(string where)
203 ActiveRecordMediator.DeleteAll(typeof(T), where);
206 /// <summary>
207 /// Saves the instance to the database
208 /// </summary>
209 /// <param name="instance"></param>
210 public static void Save(T instance)
212 ActiveRecordMediator.Save(instance);
215 /// <summary>
216 /// Saves a copy of the instance to the database
217 /// </summary>
218 /// <param name="instance"></param>
219 /// <returns>The saved instance</returns>
220 public static T SaveCopy(T instance)
222 return (T) ActiveRecordMediator.SaveCopy(instance);
225 /// <summary>
226 /// Creates (Saves) a new instance to the database.
227 /// </summary>
228 /// <param name="instance"></param>
229 public static void Create(T instance)
231 ActiveRecordMediator.Create(instance);
234 /// <summary>
235 /// Persists the modification on the instance
236 /// state to the database.
237 /// </summary>
238 /// <param name="instance"></param>
239 public static void Update(T instance)
241 ActiveRecordMediator.Update(instance);
244 /// <summary>
245 /// Deletes the instance from the database.
246 /// </summary>
247 /// <param name="instance"></param>
248 public static void Delete(T instance)
250 ActiveRecordMediator.Delete(instance);
253 /// <summary>
254 /// Refresh the instance from the database.
255 /// </summary>
256 /// <param name="instance">The ActiveRecord instance to be reloaded</param>
257 public static void Refresh(T instance)
259 ActiveRecordMediator.Refresh(instance);
262 /// <summary>
263 /// Executes the query and return a strongly typed result
264 /// </summary>
265 /// <param name="query">The query.</param>
266 /// <returns></returns>
267 public static R ExecuteQuery2<R>(IActiveRecordQuery<R> query)
269 return ActiveRecordBase<T>.ExecuteQuery2(query);
272 /// <summary>
273 /// Check if the <paramref name="id"/> exists in the database.
274 /// </summary>
275 /// <typeparam name="PkType">The <c>System.Type</c> of the PrimaryKey</typeparam>
276 /// <param name="id">The id to check on</param>
277 /// <returns><c>true</c> if the ID exists; otherwise <c>false</c>.</returns>
278 public static bool Exists<PkType>(PkType id)
280 return ActiveRecordBase<T>.Exists(id);
283 /// <summary>
284 /// Returns the number of records of the specified
285 /// type in the database
286 /// </summary>
287 /// <returns>The count result</returns>
288 public static int Count()
290 return ActiveRecordBase.Count(typeof(T));
293 /// <summary>
294 /// Returns the number of records of the specified
295 /// type in the database that match the given critera
296 /// </summary>
297 /// <param name="criteria">The criteria expression</param>
298 /// <returns>The count result</returns>
299 public static int Count(params ICriterion[] criteria)
301 return ActiveRecordBase.Count(typeof(T), criteria);
304 /// <summary>
305 /// Returns the number of records of the specified
306 /// type in the database
307 /// </summary>
308 /// <param name="filter">A sql where string i.e. Person=? and DOB &gt; ?</param>
309 /// <param name="args">Positional parameters for the filter string</param>
310 /// <returns>The count result</returns>
311 public static int Count(string filter, params object[] args)
313 return ActiveRecordBase.Count(typeof(T), filter, args);
316 /// <summary>
317 /// Returns the number of records of the specified
318 /// type in the database
319 /// </summary>
320 /// <param name="detachedCriteria">The criteria expression</param>
321 /// <returns>The count result</returns>
322 public static int Count(DetachedCriteria detachedCriteria)
324 return ActiveRecordBase.Count(typeof(T), detachedCriteria);
327 /// <summary>
328 /// Check if there is any records in the db for the target type
329 /// </summary>
330 /// <returns><c>true</c> if there's at least one row</returns>
331 public static bool Exists()
333 return ActiveRecordBase.Exists(typeof(T));
337 /// <summary>
338 /// Check if there is any records in the db for the target type
339 /// </summary>
340 /// <param name="filter">A sql where string i.e. Person=? and DOB &gt; ?</param>
341 /// <param name="args">Positional parameters for the filter string</param>
342 /// <returns><c>true</c> if there's at least one row</returns>
343 public static bool Exists(string filter, params object[] args)
345 return ActiveRecordBase.Exists(typeof(T), filter, args);
348 /// <summary>
349 /// Check if the <paramref name="id"/> exists in the database.
350 /// </summary>
351 /// <param name="id">The id to check on</param>
352 /// <returns><c>true</c> if the ID exists; otherwise <c>false</c>.</returns>
353 public static bool Exists(object id)
355 return ActiveRecordBase.Exists(typeof(T), id);
358 /// <summary>
359 /// Check if any instance matches the criteria.
360 /// </summary>
361 /// <returns><c>true</c> if an instance is found; otherwise <c>false</c>.</returns>
362 public static bool Exists(params ICriterion[] criterias)
364 return ActiveRecordBase.Exists(typeof(T), criterias);
367 /// <summary>
368 /// Check if any instance matching the criteria exists in the database.
369 /// </summary>
370 /// <param name="detachedCriteria">The criteria expression</param>
371 /// <returns><c>true</c> if an instance is found; otherwise <c>false</c>.</returns>
372 public static bool Exists(DetachedCriteria detachedCriteria)
374 return ActiveRecordBase.Exists(typeof(T), detachedCriteria);
379 #endif