UnitOfWork.cs 5.9 KB

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