using System.Reflection; using MTWorkHR.Core.UnitOfWork; using MTWorkHR.Infrastructure.Data; using MTWorkHR.Core.IRepositories; using MTWorkHR.Core.Global; using MTWorkHR.Infrastructure.Repositories; using MTWorkHR.Core.IRepositories.Base; using MTWorkHR.Identity.Entities; namespace MTWorkHR.Infrastructure.UnitOfWorks { public class UnitOfWork : IUnitOfWork { private readonly HRDataContext context; public IPermissionRepository Permission { get; } public ICompanyRepository Company { get; } public UnitOfWork(HRDataContext _context , IPermissionRepository Permission , ICompanyRepository Company ) { context = _context; this.Permission = Permission; this.Company = Company; } public async Task CompleteAsync() { try { return await context.SaveChangesAsync(); } catch (Exception ex) { RollbackTran(); throw ex; } } public object GetRepositoryByName(string name) { Type type = this.GetType(); PropertyInfo info = type.GetProperty(name); if (info == null) throw new AppException(ExceptionEnum.PropertyNotAccess, name, type.FullName); //type.FullName, String.Format("A property called {0} can't be accessed for type {1}.", name)); return info.GetValue(this, null); } public void BeginTran() { context.Database.BeginTransaction(); } public void CommitTran() { context.Database.CommitTransaction(); } public void RollbackTran() { var transaction = context.Database.CurrentTransaction; if (transaction != null) context.Database.RollbackTransaction(); } } }