UnitOfWork.cs 5.0 KB

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