UnitOfWork.cs 5.4 KB

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