UnitOfWorkRepository.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using AipGateway.API.Domain.Entities;
  2. using AipGateway.API.Domain.IRepositories.IGenericRepositories;
  3. using AipGateway.API.Infrastructure.Persistence;
  4. using Microsoft.Extensions.Logging;
  5. using AipGateway.API.Infrastructure.Configurations;
  6. namespace AipGateway.API.Infrastructure.DataAccess
  7. {
  8. public class UnitOfWorkRepository : IUnitOfWork
  9. {
  10. private readonly ILogger<UnitOfWorkRepository> _log;
  11. private readonly ApplicationDbContext _dbContext;
  12. private readonly ConnectionInfo _connectionInfo;
  13. private IGenericRepository<TbLinkedSystem>? _linkedSystemRepository;
  14. private IGenericRepository<TbLinkedServer>? _linkedServerRepository;
  15. private IGenericRepository<TbLinkedApiKey>? _linkedApiKeyRepository;
  16. private IGenericRepository<TbLinkedDecryptKey>? _linkedDescryptRepository;
  17. private IGenericRepository<TbAipLabel>? _aipAipLabelRepository;
  18. private IGenericRepository<TbAipPolicy>? _aipAipPolicyRepository;
  19. private IGenericRepository<TbAipProtection>? _aipAipProtectionRepository;
  20. private IGenericRepository<TbAipApiCallLog>? _aipApiCallLogRepository;
  21. private IGenericRepository<TbAipFileJobLog>? _aipFileJobLogRepository;
  22. public UnitOfWorkRepository(ILogger<UnitOfWorkRepository> logger, ApplicationDbContext appDbContext, ConnectionInfo connectionInfo)
  23. {
  24. _log = logger;
  25. _dbContext = appDbContext;
  26. _connectionInfo = connectionInfo;
  27. }
  28. public IGenericRepository<TbLinkedSystem> LinkedSystemRepository {
  29. get
  30. {
  31. if (_linkedSystemRepository == null)
  32. _linkedSystemRepository = new GenericRepository<TbLinkedSystem>(_dbContext, _connectionInfo);
  33. return _linkedSystemRepository;
  34. }
  35. }
  36. public IGenericRepository<TbLinkedServer> LinkedServerRepository {
  37. get
  38. {
  39. if (_linkedServerRepository == null)
  40. _linkedServerRepository = new GenericRepository<TbLinkedServer>(_dbContext, _connectionInfo);
  41. return _linkedServerRepository;
  42. }
  43. }
  44. public IGenericRepository<TbLinkedApiKey> LinkedApiKeyRepository
  45. {
  46. get
  47. {
  48. if (_linkedApiKeyRepository == null)
  49. _linkedApiKeyRepository = new GenericRepository<TbLinkedApiKey>(_dbContext, _connectionInfo);
  50. return _linkedApiKeyRepository;
  51. }
  52. }
  53. public IGenericRepository<TbLinkedDecryptKey> LinkedDecryptKeyRepository
  54. {
  55. get
  56. {
  57. if (_linkedDescryptRepository == null)
  58. _linkedDescryptRepository = new GenericRepository<TbLinkedDecryptKey>(_dbContext, _connectionInfo);
  59. return _linkedDescryptRepository;
  60. }
  61. }
  62. public IGenericRepository<TbAipLabel> AipLabelRepository
  63. {
  64. get
  65. {
  66. if (_aipAipLabelRepository == null)
  67. _aipAipLabelRepository = new GenericRepository<TbAipLabel>(_dbContext, _connectionInfo);
  68. return _aipAipLabelRepository;
  69. }
  70. }
  71. public IGenericRepository<TbAipPolicy> AipPolicyRepository
  72. {
  73. get
  74. {
  75. if (_aipAipPolicyRepository == null)
  76. _aipAipPolicyRepository = new GenericRepository<TbAipPolicy>(_dbContext, _connectionInfo);
  77. return _aipAipPolicyRepository;
  78. }
  79. }
  80. public IGenericRepository<TbAipProtection> AipProtectionRepository
  81. {
  82. get
  83. {
  84. if (_aipAipProtectionRepository == null)
  85. _aipAipProtectionRepository = new GenericRepository<TbAipProtection>(_dbContext, _connectionInfo);
  86. return _aipAipProtectionRepository;
  87. }
  88. }
  89. public IGenericRepository<TbAipApiCallLog> AipApiCallLogRepository
  90. {
  91. get
  92. {
  93. if (_aipApiCallLogRepository == null)
  94. _aipApiCallLogRepository = new GenericRepository<TbAipApiCallLog>(_dbContext, _connectionInfo);
  95. return _aipApiCallLogRepository;
  96. }
  97. }
  98. public IGenericRepository<TbAipFileJobLog> AipFileJobLogRepository
  99. {
  100. get
  101. {
  102. if (_aipFileJobLogRepository == null)
  103. _aipFileJobLogRepository = new GenericRepository<TbAipFileJobLog>(_dbContext, _connectionInfo);
  104. return _aipFileJobLogRepository;
  105. }
  106. }
  107. public int Complete()
  108. {
  109. return _dbContext.SaveChanges();
  110. }
  111. public void Dispose()
  112. {
  113. Dispose(true);
  114. GC.SuppressFinalize(this);
  115. }
  116. protected virtual void Dispose(bool disposing)
  117. {
  118. if (disposing) _dbContext.Dispose();
  119. }
  120. }
  121. }