UnitOfWork.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Reflection;
  2. using MTWorkHR.Core.UnitOfWork;
  3. using MTWorkHR.Infrastructure.DBContext;
  4. using MTWorkHR.Core.IRepositories;
  5. using MTWorkHR.Core.Global;
  6. using MTWorkHR.Infrastructure.Repositories;
  7. using MTWorkHR.Core.IRepositories.Base;
  8. using MTWorkHR.Infrastructure.Entities;
  9. namespace MTWorkHR.Infrastructure.UnitOfWorks
  10. {
  11. public class UnitOfWork : IUnitOfWork
  12. {
  13. private readonly HRDataContext context;
  14. public IPermissionRepository Permission { get; }
  15. public ICompanyRepository Company { get; }
  16. public IUserTaskRepository UserTask { get; }
  17. public ITaskStatusRepository UserTaskStatus { get; }
  18. public IUserTaskHistoryRepository UserTaskHistory { get; }
  19. public IUserTaskAttachmentRepository UserTaskAttachment { get; }
  20. public IProjectRepository Project { get; }
  21. public ITeamRepository Team{ get; }
  22. public IMeetingRepository Meeting{ get; }
  23. public IAttendanceRepository Attendance { get; }
  24. public IOrderTypeRepository OrderType { get; }
  25. public ILeaveTypeRepository LeaveType { get; }
  26. public IOrderAllocationRepository OrderAllocation { get; }
  27. public IOrderRequestRepository OrderRequest { get; }
  28. public ICountryLookupRepository CountryLookup { get; }
  29. public IIndustryRepository Industry { get; }
  30. public IJobTitleRepository JobTitle { get; }
  31. public IQualificationRepository Qualification { get; }
  32. public IUniversityRepository University { get; }
  33. public ILoginOTPRepository LoginOTP { get; }
  34. public ICityRepository City { get; }
  35. public UnitOfWork(HRDataContext _context
  36. , IPermissionRepository permission
  37. , ICompanyRepository company
  38. , IUserTaskRepository userTask
  39. , IProjectRepository project
  40. , IUserTaskHistoryRepository projectHistory
  41. , ITaskStatusRepository userTaskStatus
  42. , IUserTaskAttachmentRepository userTaskAttachment
  43. , ITeamRepository teamRepository
  44. , IMeetingRepository meetingRepository
  45. , IAttendanceRepository attendance
  46. , IOrderRequestRepository orderRequest
  47. , IOrderAllocationRepository orderAllocation
  48. , IOrderTypeRepository orderType
  49. , ILeaveTypeRepository leaveType
  50. , ICountryLookupRepository countryLookup
  51. , IIndustryRepository industry
  52. , IJobTitleRepository jobTitle
  53. , IQualificationRepository qualification
  54. , IUniversityRepository university
  55. , ILoginOTPRepository loginOTP
  56. , ICityRepository city
  57. )
  58. {
  59. context = _context;
  60. this.Permission = permission;
  61. this.Company = company;
  62. this.UserTask = userTask;
  63. this.Project = project;
  64. this.UserTaskHistory = projectHistory;
  65. this.UserTaskStatus = userTaskStatus;
  66. this.UserTaskAttachment = userTaskAttachment;
  67. this.Team = teamRepository;
  68. this.Meeting = meetingRepository;
  69. this.Attendance = attendance;
  70. this.OrderType = orderType;
  71. this.OrderRequest = orderRequest;
  72. this.OrderAllocation = orderAllocation;
  73. this.LeaveType = leaveType;
  74. this.CountryLookup = countryLookup;
  75. this.Industry = industry;
  76. this.JobTitle = jobTitle;
  77. this.Qualification = qualification;
  78. this.University = university;
  79. LoginOTP = loginOTP;
  80. City = city;
  81. }
  82. public async Task<int> CompleteAsync()
  83. {
  84. try
  85. {
  86. return await context.SaveChangesAsync();
  87. }
  88. catch (Exception ex)
  89. {
  90. RollbackTran();
  91. throw ex;
  92. }
  93. }
  94. public object GetRepositoryByName(string name)
  95. {
  96. Type type = this.GetType();
  97. PropertyInfo info = type.GetProperty(name);
  98. if (info == null)
  99. throw new AppException(ExceptionEnum.PropertyNotAccess, name, type.FullName);
  100. //type.FullName, String.Format("A property called {0} can't be accessed for type {1}.", name));
  101. return info.GetValue(this, null);
  102. }
  103. public void BeginTran()
  104. {
  105. context.Database.BeginTransaction();
  106. }
  107. public void CommitTran()
  108. {
  109. context.Database.CommitTransaction();
  110. }
  111. public void RollbackTran()
  112. {
  113. var transaction = context.Database.CurrentTransaction;
  114. if (transaction != null)
  115. context.Database.RollbackTransaction();
  116. }
  117. }
  118. }