namespace Yyw.DBUtility.NH.ActiveRecords { using System; using System.Collections; using System.Collections.Generic; using NHibernate; using NHibernate.Expression; using Yyw.DBUtility.NHibernateSessionStorage; /**////<summary> /// Base class for all ActiveRecord classes. Implements /// all the functionality to simplify the code on the /// subclasses. ///</summary> [Serializable] publicclass ActiveRecord { /**////<summary> /// Constructs an ActiveRecordBase subclass. ///</summary> public ActiveRecord() { } static methods#region static methods /**////<summary> /// Invokes the specified delegate passing a valid /// NHibernate session. Used for custom NHibernate queries. ///</summary> ///<param name="targetType">The target ActiveRecordType</param> ///<param name="call">The delegate instance</param> ///<param name="instance">The ActiveRecord instance</param> ///<returns>Whatever is returned by the delegate invocation</returns> //public static object Execute(Type targetType, NHibernateDelegate call, object instance) //{ // if (targetType == null) throw new ArgumentNullException("targetType", "Target type must be informed"); // if (call == null) throw new ArgumentNullException("call", "Delegate must be passed"); // EnsureInitialized(targetType); // ISession session = _holder.CreateSession( targetType ); // try // { // return call(session, instance); // } // catch(Exception ex) // { // throw new ActiveRecordException("Error performing Execute for " + targetType.Name, ex); // } // finally // { // _holder.ReleaseSession(session); // } //} /**////<summary> /// Finds an object instance by a unique ID ///</summary> ///<param name="targetType">The AR subclass type</param> ///<param name="id">ID value</param> ///<param name="throwOnNotFound"><c>true</c> if you want to catch an exception /// if the object is not found</param> ///<returns></returns> ///<exception cref="ObjectNotFoundException">if <c>throwOnNotFound</c> is set to ///<c>true</c> and the row is not found</exception> publicstatic T FindByPrimaryKey<T>(object id, bool throwOnNotFound) where T : class { SessionObject sessionObj = NHibernateDatabaseFactory.CreateSession(); ISession session = sessionObj.Session; try { return session.Load<T>( id ); } catch(ObjectNotFoundException ex) { if (throwOnNotFound) { String message = String.Format("Could not find {0} with id {1}", typeof(T).Name, id); thrownew NotFoundException(message, ex); } returndefault(T); } catch(Exception ex) { thrownew ActiveRecordException("Could not perform Load (Find by id) for "+typeof(T).Name, ex); } finally { if (sessionObj.IsNeedClose) session.Close(); } } /**////<summary> /// Finds an object instance by a unique ID ///</summary> ///<param name="targetType">The AR subclass type</param> ///<param name="id">ID value</param> ///<returns></returns> publicstatic T FindByPrimaryKey<T>(object id) where T : class { return FindByPrimaryKey<T>(id, true); } /**////<summary> /// Returns all instances found for the specified type. ///</summary> ///<param name="targetType"></param> ///<returns></returns> publicstatic IList<T> FindAll<T>() where T : class { return FindAll<T>((Order[]) null); } /**////<summary> /// Returns a portion of the query results (sliced) ///</summary> publicstatic IList<T> SlicedFindAll<T>(int firstResult, int maxresults, Order[] orders, params ICriterion[] criterias) where T : class { SessionObject sessionObj = NHibernateDatabaseFactory.CreateSession(); ISession session = sessionObj.Session; try { ICriteria criteria = session.CreateCriteria(typeof(T)); foreach( ICriterion cond in criterias ) { criteria.Add( cond ); }