Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / ActiveRecord / Castle.ActiveRecord / Framework / ActiveRecordMediator.cs
blob7acece5e415805e7d679a6256757d8772eedf9cb
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
17 using System;
18 using System.Collections;
19 using NHibernate;
20 using NHibernate.Expressions;
21 using Castle.ActiveRecord.Framework;
23 /// <summary>
24 /// Allow programmers to use the
25 /// ActiveRecord functionality without direct reference
26 /// to <see cref="ActiveRecordBase"/>
27 /// </summary>
28 public class ActiveRecordMediator
30 /// <summary>
31 /// Invokes the specified delegate passing a valid
32 /// NHibernate session. Used for custom NHibernate queries.
33 /// </summary>
34 /// <param name="targetType">The target ActiveRecordType</param>
35 /// <param name="call">The delegate instance</param>
36 /// <param name="instance">The ActiveRecord instance</param>
37 /// <returns>Whatever is returned by the delegate invocation</returns>
38 public static object Execute(Type targetType, NHibernateDelegate call, object instance)
40 return ActiveRecordBase.Execute(targetType, call, instance);
43 /// <summary>
44 /// Finds an object instance by its primary key.
45 /// </summary>
46 /// <param name="targetType">The AR subclass type</param>
47 /// <param name="id">ID value</param>
48 /// <param name="throwOnNotFound"><c>true</c> if you want an exception to be thrown
49 /// if the object is not found</param>
50 /// <exception cref="ObjectNotFoundException">if <c>throwOnNotFound</c> is set to
51 /// <c>true</c> and the row is not found</exception>
52 public static object FindByPrimaryKey(Type targetType, object id, bool throwOnNotFound)
54 return ActiveRecordBase.FindByPrimaryKey(targetType, id, throwOnNotFound);
57 /// <summary>
58 /// Finds an object instance by its primary key.
59 /// </summary>
60 /// <param name="targetType">The AR subclass type</param>
61 /// <param name="id">ID value</param>
62 public static object FindByPrimaryKey(Type targetType, object id)
64 return FindByPrimaryKey(targetType, id, true);
67 /// <summary>
68 /// Searches and returns the first row.
69 /// </summary>
70 /// <param name="targetType">The target type</param>
71 /// <param name="orders">The sort order - used to determine which record is the first one</param>
72 /// <param name="criterias">The criteria expression</param>
73 /// <returns>A <c>targetType</c> instance or <c>null</c></returns>
74 public static object FindFirst(Type targetType, Order[] orders, params ICriterion[] criterias)
76 return ActiveRecordBase.FindFirst(targetType, orders, criterias);
79 /// <summary>
80 /// Searches and returns the first row.
81 /// </summary>
82 /// <param name="targetType">The target type</param>
83 /// <param name="criterias">The criteria expression</param>
84 /// <returns>A <c>targetType</c> instance or <c>null</c></returns>
85 public static object FindFirst(Type targetType, params ICriterion[] criterias)
87 return FindFirst(targetType, null, criterias);
90 /// <summary>
91 /// Searches and returns the first row.
92 /// </summary>
93 /// <param name="targetType">The target type.</param>
94 /// <param name="detachedCriteria">The criteria.</param>
95 /// <param name="orders">The sort order - used to determine which record is the first one.</param>
96 /// <returns>A <c>targetType</c> instance or <c>null.</c></returns>
97 public static object FindFirst(Type targetType, DetachedCriteria detachedCriteria, params Order[] orders)
99 return ActiveRecordBase.FindFirst(targetType, detachedCriteria, orders);
102 /// <summary>
103 /// Searches and returns the first row.
104 /// </summary>
105 /// <param name="targetType">The target type</param>
106 /// <param name="criteria">The criteria expression</param>
107 /// <returns>A <c>targetType</c> instance or <c>null</c></returns>
108 public static object FindFirst(Type targetType, DetachedCriteria criteria)
110 return ActiveRecordBase.FindFirst(targetType, criteria);
113 /// <summary>
114 /// Searches and returns the a row. If more than one is found,
115 /// throws <see cref="ActiveRecordException"/>
116 /// </summary>
117 /// <param name="targetType">The target type</param>
118 /// <param name="criterias">The criteria expression</param>
119 /// <returns>A <c>targetType</c> instance or <c>null</c></returns>
120 public static object FindOne(Type targetType, params ICriterion[] criterias)
122 return ActiveRecordBase.FindOne(targetType, criterias);
125 /// <summary>
126 /// Searches and returns a row. If more than one is found,
127 /// throws <see cref="ActiveRecordException"/>
128 /// </summary>
129 /// <param name="targetType">The target type</param>
130 /// <param name="criteria">The criteria</param>
131 /// <returns>A <c>targetType</c> instance or <c>null</c></returns>
132 protected internal static object FindOne(Type targetType, DetachedCriteria criteria)
134 return ActiveRecordBase.FindOne(targetType, criteria);
137 /// <summary>
138 /// Returns a portion of the query results (sliced)
139 /// </summary>
140 public static Array SlicedFindAll(Type targetType, int firstResult, int maxresults,
141 Order[] orders, params ICriterion[] criterias)
143 return ActiveRecordBase.SlicedFindAll(targetType, firstResult,
144 maxresults, orders, criterias);
147 /// <summary>
148 /// Returns a portion of the query results (sliced)
149 /// </summary>
150 public static Array SlicedFindAll(Type targetType, int firstResult, int maxresults, params ICriterion[] criterias)
152 return SlicedFindAll(targetType, firstResult, maxresults, null, criterias);
155 /// <summary>
156 /// Returns a portion of the query results (sliced)
157 /// </summary>
158 public static Array SlicedFindAll(Type targetType, int firstResult, int maxResults,
159 Order[] orders, DetachedCriteria criteria)
161 return ActiveRecordBase.SlicedFindAll(targetType, firstResult, maxResults, orders, criteria);
164 /// <summary>
165 /// Returns a portion of the query results (sliced)
166 /// </summary>
167 public static Array SlicedFindAll(Type targetType, int firstResult, int maxResults,
168 DetachedCriteria criteria)
170 return ActiveRecordBase.SlicedFindAll(targetType, firstResult, maxResults, criteria);
173 /// <summary>
174 /// Returns all instances found for the specified type.
175 /// </summary>
176 /// <param name="targetType"></param>
177 /// <returns></returns>
178 public static Array FindAll(Type targetType)
180 return FindAll(targetType, (Order[]) null);
183 /// <summary>
184 /// Returns all instances found for the specified type
185 /// using sort orders and criterias.
186 /// </summary>
187 /// <param name="targetType"></param>
188 /// <param name="orders"></param>
189 /// <param name="criterias"></param>
190 /// <returns></returns>
191 public static Array FindAll(Type targetType, Order[] orders, params ICriterion[] criterias)
193 return ActiveRecordBase.FindAll(targetType, orders, criterias);
196 /// <summary>
197 /// Returns all instances found for the specified type
198 /// using criterias.
199 /// </summary>
200 /// <param name="targetType"></param>
201 /// <param name="criterias"></param>
202 /// <returns></returns>
203 public static Array FindAll(Type targetType, params ICriterion[] criterias)
205 return FindAll(targetType, null, criterias);
208 /// <summary>
209 /// Returns all instances found for the specified type according to the criteria
210 /// </summary>
211 public static Array FindAll(Type targetType, DetachedCriteria detachedCriteria, params Order[] orders)
213 return ActiveRecordBase.FindAll(targetType, detachedCriteria, orders);
216 /// <summary>
217 /// Finds records based on a property value - automatically converts null values to IS NULL style queries.
218 /// </summary>
219 /// <param name="targetType">The target type</param>
220 /// <param name="property">A property name (not a column name)</param>
221 /// <param name="value">The value to be equals to</param>
222 /// <returns></returns>
223 public static Array FindAllByProperty(Type targetType, String property, object value)
225 return ActiveRecordBase.FindAllByProperty(targetType, property, value);
228 /// <summary>
229 /// Finds records based on a property value - automatically converts null values to IS NULL style queries.
230 /// </summary>
231 /// <param name="targetType">The target type</param>
232 /// <param name="orderByColumn">The column name to be ordered ASC</param>
233 /// <param name="property">A property name (not a column name)</param>
234 /// <param name="value">The value to be equals to</param>
235 /// <returns></returns>
236 public static Array FindAllByProperty(Type targetType, String orderByColumn, String property, object value)
238 return ActiveRecordBase.FindAllByProperty(targetType, orderByColumn, property, value);
241 /// <summary>
242 /// Deletes all entities of the specified type (and their inheritors)
243 /// </summary>
244 /// <param name="type">The type.</param>
245 public static void DeleteAll(Type type)
247 ActiveRecordBase.DeleteAll(type);
250 /// <summary>
251 /// Deletes all entities of specified type that match the HQL where clause
252 /// </summary>
253 /// <param name="type">The type.</param>
254 /// <param name="where">The where.</param>
255 public static void DeleteAll(Type type, string where)
257 ActiveRecordBase.DeleteAll(type, where);
260 /// <summary>
261 /// Deletes all <paramref name="targetType" /> objects, based on the primary keys
262 /// supplied on <paramref name="pkValues" />.
263 /// </summary>
264 /// <param name="targetType">The target ActiveRecord type</param>
265 /// <param name="pkValues">A list of primary keys</param>
266 /// <returns>The number of objects deleted</returns>
267 public static int DeleteAll(Type targetType, IEnumerable pkValues)
269 return ActiveRecordBase.DeleteAll(targetType, pkValues);
272 /// <summary>
273 /// Enumerates the query.
274 /// Note: Only use if you expect most of the values to be on the second level cache
275 /// </summary>
276 /// <param name="q">The query</param>
277 /// <returns></returns>
278 public static IEnumerable EnumerateQuery(IActiveRecordQuery q)
280 return ActiveRecordBase.EnumerateQuery(q);
283 /// <summary>
284 /// Executes the query
285 /// </summary>
286 /// <param name="q">The query</param>
287 /// <returns></returns>
288 public static object ExecuteQuery(IActiveRecordQuery q)
290 return ActiveRecordBase.ExecuteQuery(q);
293 /// <summary>
294 /// Returns the number of records of the specified
295 /// type in the database
296 /// </summary>
297 /// <example>
298 /// <code>
299 /// [ActiveRecord]
300 /// public class User : ActiveRecordBase
301 /// {
302 /// ...
303 ///
304 /// public static int CountUsers()
305 /// {
306 /// return Count(typeof(User));
307 /// }
308 /// }
309 /// </code>
310 /// </example>
311 /// <param name="targetType">Type of the target.</param>
312 /// <returns>The count result</returns>
313 protected internal static int Count(Type targetType)
315 return ActiveRecordBase.Count(targetType);
318 /// <summary>
319 /// Returns the number of records of the specified
320 /// type in the database
321 /// </summary>
322 /// <example>
323 /// <code>
324 /// [ActiveRecord]
325 /// public class User : ActiveRecordBase
326 /// {
327 /// ...
328 ///
329 /// public static int CountUsersLocked()
330 /// {
331 /// return Count(typeof(User), "IsLocked = ?", true);
332 /// }
333 /// }
334 /// </code>
335 /// </example>
336 /// <param name="targetType">Type of the target.</param>
337 /// <param name="filter">A sql where string i.e. Person=? and DOB &gt; ?</param>
338 /// <param name="args">Positional parameters for the filter string</param>
339 /// <returns>The count result</returns>
340 public static int Count(Type targetType, string filter, params object[] args)
342 return ActiveRecordBase.Count(targetType, filter, args);
345 /// <summary>
346 /// Returns the number of records of the specified
347 /// type in the database
348 /// </summary>
349 /// <param name="targetType">The target type.</param>
350 /// <param name="criteria">The criteria expression</param>
351 /// <returns>The count result</returns>
352 public static int Count(Type targetType, params ICriterion[] criteria) {
353 return ActiveRecordBase.Count(targetType, criteria);
356 /// <summary>
357 /// Returns the number of records of the specified
358 /// type in the database
359 /// </summary>
360 /// <param name="targetType">The target type.</param>
361 /// <param name="detachedCriteria">The criteria expression</param>
362 /// <returns>The count result</returns>
363 public static int Count(Type targetType, DetachedCriteria detachedCriteria)
365 return ActiveRecordBase.Count(targetType, detachedCriteria);
368 /// <summary>
369 /// Check if there is any records in the db for the target type
370 /// </summary>
371 /// <param name="targetType">Type of the target.</param>
372 /// <returns><c>true</c> if there's at least one row</returns>
373 public static bool Exists(Type targetType)
375 return ActiveRecordBase.Exists(targetType);
379 /// <summary>
380 /// Check if there is any records in the db for the target type
381 /// </summary>
382 /// <param name="targetType">Type of the target.</param>
383 /// <param name="filter">A sql where string i.e. Person=? and DOB &gt; ?</param>
384 /// <param name="args">Positional parameters for the filter string</param>
385 /// <returns><c>true</c> if there's at least one row</returns>
386 public static bool Exists(Type targetType, string filter, params object[] args)
388 return ActiveRecordBase.Exists(targetType, filter, args);
391 /// <summary>
392 /// Check if the <paramref name="id"/> exists in the database.
393 /// </summary>
394 /// <param name="targetType">Type of the target.</param>
395 /// <param name="id">The id to check on</param>
396 /// <returns><c>true</c> if the ID exists; otherwise <c>false</c>.</returns>
397 public static bool Exists(Type targetType, object id)
399 return ActiveRecordBase.Exists(targetType, id);
402 /// <summary>
403 /// Check if any instance matches the criteria.
404 /// </summary>
405 /// <returns><c>true</c> if an instance is found; otherwise <c>false</c>.</returns>
406 public static bool Exists(Type targetType, params ICriterion[] criterias)
408 return ActiveRecordBase.Exists(targetType, criterias);
411 /// <summary>
412 /// Check if any instance matching the criteria exists in the database.
413 /// </summary>
414 /// <param name="targetType">The target type.</param>
415 /// <param name="detachedCriteria">The criteria expression</param>
416 /// <returns><c>true</c> if an instance is found; otherwise <c>false</c>.</returns>
417 public static bool Exists(Type targetType, DetachedCriteria detachedCriteria)
419 return ActiveRecordBase.Exists(targetType, detachedCriteria);
422 /// <summary>
423 /// Saves the instance to the database
424 /// </summary>
425 /// <param name="instance">The ActiveRecord instance to be deleted</param>
426 public static void Save(object instance)
428 ActiveRecordBase.Save(instance);
431 /// <summary>
432 /// Saves the instance to the database and flushes the session. If the primary key is unitialized
433 /// it creates the instance on the database. Otherwise it updates it.
434 /// <para>
435 /// If the primary key is assigned, then you must invoke <see cref="Create(object)"/>
436 /// or <see cref="Update(object)"/> instead.
437 /// </para>
438 /// </summary>
439 /// <param name="instance">The ActiveRecord instance to be saved</param>
440 public static void SaveAndFlush(object instance)
442 ActiveRecordBase.SaveAndFlush(instance);
445 /// <summary>
446 /// Saves a copy of instance to the database
447 /// </summary>
448 /// <param name="instance">The transient instance to be copied</param>
449 /// <returns>The saved ActiveRecord instance</returns>
450 public static object SaveCopy(object instance)
452 return ActiveRecordBase.SaveCopy(instance);
455 /// <summary>
456 /// Saves a copy of the instance to the database and flushes the session. If the primary key is unitialized
457 /// it creates the instance on the database. Otherwise it updates it.
458 /// <para>
459 /// If the primary key is assigned, then you must invoke <see cref="Create(object)"/>
460 /// or <see cref="Update(object)"/> instead.
461 /// </para>
462 /// </summary>
463 /// <param name="instance">The transient instance to be copied</param>
464 /// <returns>The saved ActiveRecord instance</returns>
465 public static void SaveCopyAndFlush(object instance)
467 ActiveRecordBase.SaveCopyAndFlush(instance);
470 /// <summary>
471 /// Creates (Saves) a new instance to the database.
472 /// </summary>
473 /// <param name="instance">The ActiveRecord instance to be deleted</param>
474 public static void Create(object instance)
476 ActiveRecordBase.Create(instance);
479 /// <summary>
480 /// Creates (Saves) a new instance to the database and flushes the session.
481 /// </summary>
482 /// <param name="instance">The ActiveRecord instance to be created on the database</param>
483 public static void CreateAndFlush(object instance)
485 ActiveRecordBase.CreateAndFlush(instance);
488 /// <summary>
489 /// Persists the modification on the instance
490 /// state to the database.
491 /// </summary>
492 /// <param name="instance">The ActiveRecord instance to be deleted</param>
493 public static void Update(object instance)
495 ActiveRecordBase.Update(instance);
498 /// <summary>
499 /// Persists the modification on the instance
500 /// state to the database and flushes the session.
501 /// </summary>
502 /// <param name="instance">The ActiveRecord instance to be updated on the database</param>
503 public static void UpdateAndFlush(object instance)
505 ActiveRecordBase.UpdateAndFlush(instance);
508 /// <summary>
509 /// Deletes the instance from the database.
510 /// </summary>
511 /// <param name="instance">The ActiveRecord instance to be deleted</param>
512 public static void Delete(object instance)
514 ActiveRecordBase.Delete(instance);
517 /// <summary>
518 /// Deletes the instance from the database and flushes the session.
519 /// </summary>
520 /// <param name="instance">The ActiveRecord instance to be deleted</param>
521 public static void DeleteAndFlush(object instance)
523 ActiveRecordBase.DeleteAndFlush(instance);
526 /// <summary>
527 /// Refresh the instance from the database.
528 /// </summary>
529 /// <param name="instance">The ActiveRecord instance to be reloaded</param>
530 public static void Refresh(object instance)
532 ActiveRecordBase.Refresh(instance);
535 /// <summary>
536 /// Testing hock only.
537 /// </summary>
538 public static ISessionFactoryHolder GetSessionFactoryHolder()
540 return ActiveRecordBase.holder;
543 /// <summary>
544 /// From NHibernate documentation:
545 /// Persist all reachable transient objects, reusing the current identifier
546 /// values. Note that this will not trigger the Interceptor of the Session.
547 /// </summary>
548 /// <param name="instance">The instance.</param>
549 /// <param name="replicationMode">The replication mode.</param>
550 public static void Replicate(object instance, ReplicationMode replicationMode)
552 ActiveRecordBase.Replicate(instance, replicationMode);
555 /// <summary>
556 /// Evicts the specified instance from the first level cache (session level).
557 /// </summary>
558 /// <param name="instance">The instance.</param>
559 public static void Evict(object instance)
561 if (instance == null) throw new ArgumentNullException("instance");
563 if (SessionScope.Current != null)
565 SessionScope.Current.Evict(instance);
569 /// <summary>
570 /// Evicts the specified type.
571 /// </summary>
572 /// <param name="type">The type.</param>
573 public static void GlobalEvict(Type type)
575 if (type == null) throw new ArgumentNullException("type");
577 ISessionFactory factory = GetFactory(type);
579 factory.Evict(type);
582 /// <summary>
583 /// Evicts the specified type.
584 /// </summary>
585 /// <param name="type">The type.</param>
586 /// <param name="id">The id.</param>
587 public static void GlobalEvict(Type type, object id)
589 if (type == null) throw new ArgumentNullException("type");
591 ISessionFactory factory = ActiveRecordBase.holder.GetSessionFactory(type);
593 if (factory == null)
595 throw new ActiveRecordException("Could not find registered session factory for type " + type.FullName);
598 factory.Evict(type, id);
601 private static ISessionFactory GetFactory(Type type)
603 ISessionFactory factory = ActiveRecordBase.holder.GetSessionFactory(type);
605 if (factory == null)
607 throw new ActiveRecordException("Could not find registered session factory for type " + type.FullName);
609 return factory;