UnitOfWork.cs 5.1 KB

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