UnitOfWork.cs 3.7 KB

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