UnitOfWork.cs 4.8 KB

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