/// <summary> /// Runs a functor inside a transaction /// </summary> /// <typeparam name="TResult">Type of the result of the functor</typeparam> /// <param name="storage">Storage to use</param> /// <param name="functor">Functor to call</param> /// <returns>Result of the functor</returns> public static TResult AutoCommit <TResult>(this ISessionFactory storage, Func <ISession, TResult> functor) { var result = default(TResult); storage.AutoSession(s => { using (var tx = s.BeginTransaction()) { try { result = functor(s); tx.Commit(); } catch { tx.Rollback(); throw; } } }); return(result); }
/// <summary> /// Lists all the elements of type T in the storage /// </summary> /// <typeparam name="T">Type of the element to look for</typeparam> /// <param name="factory">Factory to open the session</param> /// <returns>A collection with all the elements</returns> public static IList <T> List <T>(this ISessionFactory factory) { return(factory .AutoSession(s => s .CreateCriteria(typeof(T)) .List <T>())); }
/// <summary> /// Runs an action inside a transaction /// </summary> /// <param name="storage">Storage to use</param> /// <param name="action">Action to call</param> public static void AutoCommit(this ISessionFactory storage, Action <ISession> action) { storage.AutoSession(s => { using (var tx = s.BeginTransaction()) { try { action(s); tx.Commit(); } catch { tx.Rollback(); throw; } } }); }