UnitOfWork.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Microsoft.EntityFrameworkCore;
  2. using ProductionLineMonitor.Core.IRepositories;
  3. using ProductionLineMonitor.Core.Models;
  4. using System.Collections;
  5. using System;
  6. namespace ProductionLineMonitor.EntityFramework.Repositories
  7. {
  8. public class UnitOfWork : IUnitOfWork
  9. {
  10. private readonly ProductionLineContext _context;
  11. public UnitOfWork(ProductionLineContext context)
  12. {
  13. _context = context;
  14. }
  15. private readonly IBaseRepository<User>? _userRepository = null;
  16. public IBaseRepository<User> UserRepository =>
  17. _userRepository ?? new BaseRepository<User>(_context);
  18. private readonly IBaseRepository<Role>? _roleRepository = null;
  19. public IBaseRepository<Role> RoleRepository =>
  20. _roleRepository ?? new BaseRepository<Role>(_context);
  21. private readonly IBaseRepository<Machine>? _machineRepository = null;
  22. public IBaseRepository<Machine> MachineRepository =>
  23. _machineRepository ?? new BaseRepository<Machine>(_context);
  24. private readonly IBaseRepository<MachineOutPutPerHour>? _machineOutPutPerHourRepository = null;
  25. public IBaseRepository<MachineOutPutPerHour> MachineOutPutPerHourRepository =>
  26. _machineOutPutPerHourRepository ?? new BaseRepository<MachineOutPutPerHour>(_context);
  27. private readonly IBaseRepository<ProductionLine>? _productionLineRepository = null;
  28. public IBaseRepository<ProductionLine> ProductionLineRepository =>
  29. _productionLineRepository ?? new BaseRepository<ProductionLine>(_context);
  30. private readonly IBaseRepository<Recipe>? _recipeRepository = null;
  31. public IBaseRepository<Recipe> RecipeRepository =>
  32. _recipeRepository ?? new BaseRepository<Recipe>(_context);
  33. private readonly IBaseRepository<MachineFaultComparison>? _machineFaultComparisonRepository = null;
  34. public IBaseRepository<MachineFaultComparison> MachineFaultComparisonRepository =>
  35. _machineFaultComparisonRepository ?? new BaseRepository<MachineFaultComparison>(_context);
  36. private readonly IBaseRepository<MachineFaultRecord>? _machineFaultRecordRepository = null;
  37. public IBaseRepository<MachineFaultRecord> MachineFaultRecordRepository =>
  38. _machineFaultRecordRepository ?? new BaseRepository<MachineFaultRecord>(_context);
  39. private readonly IBaseRepository<Cim>? _cimRepository = null;
  40. public IBaseRepository<Cim> CimRepository =>
  41. _cimRepository ?? new BaseRepository<Cim>(_context);
  42. private readonly IBaseRepository<MachineStatistic>? _machineStatisticRepository = null;
  43. public IBaseRepository<MachineStatistic> MachineStatisticRepository =>
  44. _machineStatisticRepository ?? new BaseRepository<MachineStatistic>(_context);
  45. private readonly IBaseRepository<MachinePowerConsumption>? _machinePowerConsumptionRepository = null;
  46. public IBaseRepository<MachinePowerConsumption> MachinePowerConsumptionRepository =>
  47. _machinePowerConsumptionRepository ?? new BaseRepository<MachinePowerConsumption>(_context);
  48. private readonly IBaseRepository<MachineProcessPS>? _machineProcessPSRepository = null;
  49. public IBaseRepository<MachineProcessPS> MachineProcessPSRepository =>
  50. _machineProcessPSRepository ?? new BaseRepository<MachineProcessPS>(_context);
  51. private readonly IBaseRepository<MachineProcessFOG>? _machineProcessFOGRepository = null;
  52. public IBaseRepository<MachineProcessFOG> MachineProcessFOGRepository =>
  53. _machineProcessFOGRepository ?? new BaseRepository<MachineProcessFOG>(_context);
  54. private readonly IBaseRepository<MachineProcessBT>? _machineProcessBTRepository = null;
  55. public IBaseRepository<MachineProcessBT> MachineProcessBTRepository =>
  56. _machineProcessBTRepository ?? new BaseRepository<MachineProcessBT>(_context);
  57. private readonly IBaseRepository<MachineProcessPSEPD>? _machineProcessPSEPDRepository = null;
  58. public IBaseRepository<MachineProcessPSEPD> MachineProcessPSEPDRepository =>
  59. _machineProcessPSEPDRepository ?? new BaseRepository<MachineProcessPSEPD>(_context);
  60. private readonly IBaseRepository<RoleMenu>? _roleMenuRepository = null;
  61. public IBaseRepository<RoleMenu> RoleMenuRepository =>
  62. _roleMenuRepository ?? new BaseRepository<RoleMenu>(_context);
  63. private readonly IBaseRepository<Menu>? _menuRepository = null;
  64. public IBaseRepository<Menu> MenuRepository =>
  65. _menuRepository ?? new BaseRepository<Menu>(_context);
  66. private readonly IBaseRepository<ElectricEnergyMeter>? _electricEnergyMeterRepository = null;
  67. public IBaseRepository<ElectricEnergyMeter> ElectricEnergyMeterRepository =>
  68. _electricEnergyMeterRepository ?? new BaseRepository<ElectricEnergyMeter>(_context);
  69. private readonly IBaseRepository<ElectricEnergyMeterLog>? _electricEnergyMeterLogRepository = null;
  70. public IBaseRepository<ElectricEnergyMeterLog> ElectricEnergyMeterLogRepository =>
  71. _electricEnergyMeterLogRepository ?? new BaseRepository<ElectricEnergyMeterLog>(_context);
  72. private readonly IBaseRepository<EmailInfo>? _emailInfoRepository = null;
  73. public IBaseRepository<EmailInfo> EmailInfoRepository =>
  74. _emailInfoRepository ?? new BaseRepository<EmailInfo>(_context);
  75. public void Dispose()
  76. {
  77. _context?.Dispose();
  78. }
  79. public int SaveChanges()
  80. {
  81. return _context.SaveChanges();
  82. }
  83. public void ExecuteSql(string sql, params object[] sqlParams)
  84. {
  85. _context.Database.ExecuteSqlRaw(sql, sqlParams);
  86. }
  87. private Hashtable? repositorys;
  88. public IBaseRepository<TEntity> Repository<TEntity>() where TEntity : Base
  89. {
  90. repositorys ??= new Hashtable();
  91. var entityType = typeof(TEntity);
  92. if (!repositorys.ContainsKey(entityType.Name))
  93. {
  94. var baseType = typeof(BaseRepository<>);
  95. var repositoryInstance = Activator.CreateInstance(baseType.MakeGenericType(entityType), _context);
  96. repositorys.Add(entityType.Name, repositoryInstance);
  97. }
  98. return (IBaseRepository<TEntity>)repositorys[entityType.Name];
  99. }
  100. public DbContext GetDbContext()
  101. {
  102. return _context;
  103. }
  104. }
  105. }