UnitOfWork.cs 5.5 KB

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