UnitOfWork.cs 4.5 KB

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