1 // Copyright 2004-2007 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.
17 namespace Castle
.ActiveRecord
20 using NHibernate
.Expression
;
23 /// Allow programmers to use the
24 /// ActiveRecord functionality without extending <see cref="ActiveRecordBase"/>
26 public class ActiveRecordMediator
<T
> : ActiveRecordMediator where T
: class
29 /// Invokes the specified delegate passing a valid
30 /// NHibernate session. Used for custom NHibernate queries.
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
);
41 /// Finds an object instance by its primary key.
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
);
54 /// Finds an object instance by its primary key.
56 /// <param name="id">ID value</param>
57 public static T
FindByPrimaryKey(object id
)
59 return (T
) ActiveRecordMediator
.FindByPrimaryKey(typeof(T
), id
, true);
63 /// Searches and returns the first row.
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
);
74 /// Searches and returns the first row.
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
);
84 /// Searches and returns the first row.
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
);
95 /// Searches and returns the first row.
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
);
105 /// Searches and returns the first row.
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
);
115 /// Searches and returns a row. If more than one is found,
116 /// throws <see cref="ActiveRecordException"/>
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
);
126 /// Returns all instances found for the specified type.
128 /// <returns></returns>
129 public static T
[] FindAll()
131 return (T
[]) ActiveRecordMediator
.FindAll(typeof(T
));
135 /// Returns all instances found for the specified type
136 /// using sort orders and criterias.
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
);
147 /// Returns all instances found for the specified type
150 /// <param name="criterias"></param>
151 /// <returns></returns>
152 public static T
[] FindAll(params ICriterion
[] criterias
)
154 return (T
[])ActiveRecordMediator
.FindAll(typeof(T
), criterias
);
158 /// Returns all instances found for the specified type according to the criteria
160 public static T
[] FindAll(DetachedCriteria detachedCriteria
, params Order
[] orders
)
162 return (T
[])ActiveRecordMediator
.FindAll(typeof(T
), detachedCriteria
, orders
);
166 /// Returns a portion of the query results (sliced)
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
);
174 /// Returns a portion of the query results (sliced)
176 public static T
[] SlicedFindAll(int firstResult
, int maxResults
, params ICriterion
[] criterias
)
178 return (T
[]) ActiveRecordMediator
.SlicedFindAll(typeof(T
), firstResult
, maxResults
, null, criterias
);
182 /// Returns a portion of the query results (sliced)
184 public static T
[] SlicedFindAll(int firstResult
, int maxResults
,
185 DetachedCriteria criteria
)
187 return (T
[])ActiveRecordMediator
.SlicedFindAll(typeof(T
), firstResult
, maxResults
, criteria
);
191 /// Deletes all entities of <typeparamref name="T"/>.
193 public static void DeleteAll()
195 ActiveRecordMediator
.DeleteAll(typeof(T
));
199 /// Deletes all entities of <typeparamref name="T"/> that match the HQL where clause.
201 public static void DeleteAll(string where
)
203 ActiveRecordMediator
.DeleteAll(typeof(T
), where
);
207 /// Saves the instance to the database
209 /// <param name="instance"></param>
210 public static void Save(T instance
)
212 ActiveRecordMediator
.Save(instance
);
216 /// Saves a copy of the instance to the database
218 /// <param name="instance"></param>
219 /// <returns>The saved instance</returns>
220 public static T
SaveCopy(T instance
)
222 return (T
) ActiveRecordMediator
.SaveCopy(instance
);
226 /// Creates (Saves) a new instance to the database.
228 /// <param name="instance"></param>
229 public static void Create(T instance
)
231 ActiveRecordMediator
.Create(instance
);
235 /// Persists the modification on the instance
236 /// state to the database.
238 /// <param name="instance"></param>
239 public static void Update(T instance
)
241 ActiveRecordMediator
.Update(instance
);
245 /// Deletes the instance from the database.
247 /// <param name="instance"></param>
248 public static void Delete(T instance
)
250 ActiveRecordMediator
.Delete(instance
);
254 /// Refresh the instance from the database.
256 /// <param name="instance">The ActiveRecord instance to be reloaded</param>
257 public static void Refresh(T instance
)
259 ActiveRecordMediator
.Refresh(instance
);
263 /// Executes the query and return a strongly typed result
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
);
273 /// Check if the <paramref name="id"/> exists in the database.
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
);
284 /// Returns the number of records of the specified
285 /// type in the database
287 /// <returns>The count result</returns>
288 public static int Count()
290 return ActiveRecordBase
.Count(typeof(T
));
294 /// Returns the number of records of the specified
295 /// type in the database that match the given critera
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
);
305 /// Returns the number of records of the specified
306 /// type in the database
308 /// <param name="filter">A sql where string i.e. Person=? and DOB > ?</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
);
317 /// Returns the number of records of the specified
318 /// type in the database
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
);
328 /// Check if there is any records in the db for the target type
330 /// <returns><c>true</c> if there's at least one row</returns>
331 public static bool Exists()
333 return ActiveRecordBase
.Exists(typeof(T
));
338 /// Check if there is any records in the db for the target type
340 /// <param name="filter">A sql where string i.e. Person=? and DOB > ?</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
);
349 /// Check if the <paramref name="id"/> exists in the database.
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
);
359 /// Check if any instance matches the criteria.
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
);
368 /// Check if any instance matching the criteria exists in the database.
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
);