예제 #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;
                }
            }
        }
예제 #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;
                }
            }
        }
예제 #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;
                }
            }
        }