UnitOfWork.cs 5.2 KB

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