UnitOfWork.cs 4.2 KB

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