using Microsoft.EntityFrameworkCore; using ProductionLineMonitor.Core.IRepositories; using ProductionLineMonitor.Core.Models; using System.Collections; using System; namespace ProductionLineMonitor.EntityFramework.Repositories { public class UnitOfWork : IUnitOfWork { private readonly ProductionLineContext _context; public UnitOfWork(ProductionLineContext context) { _context = context; } private readonly IBaseRepository? _userRepository = null; public IBaseRepository UserRepository => _userRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _roleRepository = null; public IBaseRepository RoleRepository => _roleRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _machineRepository = null; public IBaseRepository MachineRepository => _machineRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _machineOutPutPerHourRepository = null; public IBaseRepository MachineOutPutPerHourRepository => _machineOutPutPerHourRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _productionLineRepository = null; public IBaseRepository ProductionLineRepository => _productionLineRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _recipeRepository = null; public IBaseRepository RecipeRepository => _recipeRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _machineFaultComparisonRepository = null; public IBaseRepository MachineFaultComparisonRepository => _machineFaultComparisonRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _machineFaultRecordRepository = null; public IBaseRepository MachineFaultRecordRepository => _machineFaultRecordRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _cimRepository = null; public IBaseRepository CimRepository => _cimRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _machineStatisticRepository = null; public IBaseRepository MachineStatisticRepository => _machineStatisticRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _machinePowerConsumptionRepository = null; public IBaseRepository MachinePowerConsumptionRepository => _machinePowerConsumptionRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _machineProcessPSRepository = null; public IBaseRepository MachineProcessPSRepository => _machineProcessPSRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _machineProcessFOGRepository = null; public IBaseRepository MachineProcessFOGRepository => _machineProcessFOGRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _machineProcessBTRepository = null; public IBaseRepository MachineProcessBTRepository => _machineProcessBTRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _machineProcessPSEPDRepository = null; public IBaseRepository MachineProcessPSEPDRepository => _machineProcessPSEPDRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _roleMenuRepository = null; public IBaseRepository RoleMenuRepository => _roleMenuRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _menuRepository = null; public IBaseRepository MenuRepository => _menuRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _electricEnergyMeterRepository = null; public IBaseRepository ElectricEnergyMeterRepository => _electricEnergyMeterRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _electricEnergyMeterLogRepository = null; public IBaseRepository ElectricEnergyMeterLogRepository => _electricEnergyMeterLogRepository ?? new BaseRepository(_context); private readonly IBaseRepository? _emailInfoRepository = null; public IBaseRepository EmailInfoRepository => _emailInfoRepository ?? new BaseRepository(_context); public void Dispose() { _context?.Dispose(); } public int SaveChanges() { return _context.SaveChanges(); } public void ExecuteSql(string sql, params object[] sqlParams) { _context.Database.ExecuteSqlRaw(sql, sqlParams); } private Hashtable? repositorys; public IBaseRepository Repository() where TEntity : Base { repositorys ??= new Hashtable(); var entityType = typeof(TEntity); if (!repositorys.ContainsKey(entityType.Name)) { var baseType = typeof(BaseRepository<>); var repositoryInstance = Activator.CreateInstance(baseType.MakeGenericType(entityType), _context); repositorys.Add(entityType.Name, repositoryInstance); } return (IBaseRepository)repositorys[entityType.Name]; } public DbContext GetDbContext() { return _context; } } }