UnitOfWork.cs 6.2 KB

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