Example #1
0
        public static void DoDapperService(this IServiceProvider serviceProvider, DbConfig dbConfig, Action <IUnitOfWork> action)
        {
            using (DatabaseSession databaseSession = new DatabaseSession(dbConfig))
            {
                IUnitOfWork unitOfWork = databaseSession.UnitOfWork;
                unitOfWork.Begin();

                try
                {
                    action(unitOfWork);
                    unitOfWork.Commit();
                }
                catch
                {
                    unitOfWork.Rollback();
                    throw;
                }
            }
        }
Example #2
0
        public static async Task DoDapperServiceAsync(this IServiceProvider serviceProvider, DbConfig dbConfig, Func <IUnitOfWork, Task> action)
        {
            using (DatabaseSession databaseSession = new DatabaseSession(dbConfig))
            {
                IUnitOfWork unitOfWork = databaseSession.UnitOfWork;
                unitOfWork.Begin();

                try
                {
                    await action(unitOfWork);

                    unitOfWork.Commit();
                }
                catch
                {
                    unitOfWork.Rollback();
                    throw;
                }
            }
        }
Example #3
0
        public static TResult DoDapperService <TResult>(this IServiceProvider serviceProvider, DbConfig dbConfig, Func <IUnitOfWork, TResult> action)
        {
            using (DatabaseSession databaseSession = new DatabaseSession(dbConfig))
            {
                IUnitOfWork unitOfWork = databaseSession.UnitOfWork;
                unitOfWork.Begin();

                try
                {
                    var result = action(unitOfWork);
                    unitOfWork.Commit();
                    return(result);
                }
                catch
                {
                    unitOfWork.Rollback();
                    throw;
                }
            }
        }