public async static Task <T> MannualTransaction <T>(this IRepository Repository, Func <Task <T> > action) { if (action == null || Repository.UnitOfWork == null) { return(default(T)); } var UnitOfWork = Repository.UnitOfWork; T result; Exception exception = null; using (DalSession dalSession = new DalSession(UnitOfWork)) { try { result = await action(); } catch (Exception ex) { UnitOfWork.Rollback(); result = default(T); exception = ex; } } if (exception != null) { throw new Exception("Query DB failed!", exception); } return(result); }
public async static Task Transaction(this IRepository Repository, Func <Task> action) { if (action == null || Repository.UnitOfWork == null) { return; } var UnitOfWork = Repository.UnitOfWork; Exception exception = null; using (DalSession dalSession = new DalSession(UnitOfWork)) { UnitOfWork.Begin(); try { await action(); UnitOfWork.Commit(); } catch (Exception ex) { UnitOfWork.Rollback(); exception = ex; } } if (exception != null) { throw new Exception("Query DB failed!", exception); } }