From a58e6c5108d205f0ab416eb534bf2ce8536dccb5 Mon Sep 17 00:00:00 2001 From: Andrii Podkolzin Date: Fri, 10 Feb 2023 11:48:02 +0200 Subject: [PATCH] Improve DI registration + Modify UnitOfWork to use Repos from DI --- Pds/Pds.Data/AssemblyRunner.cs | 4 -- Pds/Pds.Data/IUnitOfWork.cs | 5 +- Pds/Pds.Data/UnitOfWork.cs | 92 ++++++++++++++---------------- Pds/Pds.Di/ApiDiModule.cs | 27 +++------ Pds/Pds.Services/AssemblyRunner.cs | 4 -- 5 files changed, 54 insertions(+), 78 deletions(-) diff --git a/Pds/Pds.Data/AssemblyRunner.cs b/Pds/Pds.Data/AssemblyRunner.cs index 96fb7c4e..751a8baf 100644 --- a/Pds/Pds.Data/AssemblyRunner.cs +++ b/Pds/Pds.Data/AssemblyRunner.cs @@ -2,8 +2,4 @@ public static class AssemblyRunner { - public static void Run() - { - - } } \ No newline at end of file diff --git a/Pds/Pds.Data/IUnitOfWork.cs b/Pds/Pds.Data/IUnitOfWork.cs index 2f653d2c..cd941ee0 100644 --- a/Pds/Pds.Data/IUnitOfWork.cs +++ b/Pds/Pds.Data/IUnitOfWork.cs @@ -3,7 +3,7 @@ namespace Pds.Data; -public interface IUnitOfWork : IDisposable +public interface IUnitOfWork { IPersonRepository Persons { get; } IResourceRepository Resources { get; } @@ -15,7 +15,6 @@ public interface IUnitOfWork : IDisposable IGiftRepository Gifts { get; } ISettingRepository Settings { get; } IDashboardRepository Dashboard { get; } - - void Save(); + EntityEntry GetContextEntry(object obj); } \ No newline at end of file diff --git a/Pds/Pds.Data/UnitOfWork.cs b/Pds/Pds.Data/UnitOfWork.cs index 1f0c16d8..b855f185 100644 --- a/Pds/Pds.Data/UnitOfWork.cs +++ b/Pds/Pds.Data/UnitOfWork.cs @@ -7,61 +7,57 @@ namespace Pds.Data; public sealed class UnitOfWork : IUnitOfWork { private readonly ApplicationDbContext context; - private IPersonRepository personRepository; - private IResourceRepository resourceRepository; - private IContentRepository contentRepository; - private IBrandRepository brandRepository; - private IClientRepository clientRepository; - private IBillRepository billRepository; - private ICostRepository costRepository; - private IGiftRepository giftRepository; - private ISettingRepository settingRepository; - private IDashboardRepository dashboardRepository; - - public UnitOfWork(ApplicationDbContext context) + private readonly Lazy personRepository; + private readonly Lazy resourceRepository; + private readonly Lazy contentRepository; + private readonly Lazy brandRepository; + private readonly Lazy clientRepository; + private readonly Lazy billRepository; + private readonly Lazy costRepository; + private readonly Lazy giftRepository; + private readonly Lazy settingRepository; + private readonly Lazy dashboardRepository; + + public UnitOfWork( + ApplicationDbContext context, + Lazy personRepository, + Lazy resourceRepository, + Lazy contentRepository, + Lazy brandRepository, + Lazy clientRepository, + Lazy billRepository, + Lazy costRepository, + Lazy giftRepository, + Lazy settingRepository, + Lazy dashboardRepository + ) { this.context = context; + this.personRepository = personRepository; + this.resourceRepository = resourceRepository; + this.contentRepository = contentRepository; + this.brandRepository = brandRepository; + this.clientRepository = clientRepository; + this.billRepository = billRepository; + this.costRepository = costRepository; + this.giftRepository = giftRepository; + this.settingRepository = settingRepository; + this.dashboardRepository = dashboardRepository; } - public IPersonRepository Persons => personRepository ??= new PersonRepository(context); - public IResourceRepository Resources => resourceRepository ??= new ResourceRepository(context); - public IContentRepository Content => contentRepository ??= new ContentRepository(context); - public IBrandRepository Brands => brandRepository ??= new BrandRepository(context); - public IClientRepository Clients => clientRepository ??= new ClientRepository(context); - public IBillRepository Bills => billRepository ??= new BillRepository(context); - public ICostRepository Costs => costRepository ??= new CostRepository(context); - public IGiftRepository Gifts => giftRepository ??= new GiftRepository(context); - public ISettingRepository Settings => settingRepository ??= new SettingRepository(context); - public IDashboardRepository Dashboard => dashboardRepository ??= new DashboardRepository(context); - - public void Save() - { - context.SaveChanges(); - } - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } + public IPersonRepository Persons => personRepository.Value; + public IResourceRepository Resources => resourceRepository.Value; + public IContentRepository Content => contentRepository.Value; + public IBrandRepository Brands => brandRepository.Value; + public IClientRepository Clients => clientRepository.Value; + public IBillRepository Bills => billRepository.Value; + public ICostRepository Costs => costRepository.Value; + public IGiftRepository Gifts => giftRepository.Value; + public ISettingRepository Settings => settingRepository.Value; + public IDashboardRepository Dashboard => dashboardRepository.Value; public EntityEntry GetContextEntry(object obj) { return context.Entry(obj); } - - private bool disposed = false; - - private void Dispose(bool disposing) - { - if (!this.disposed) - { - if (disposing) - { - context.Dispose(); - } - } - - this.disposed = true; - } } \ No newline at end of file diff --git a/Pds/Pds.Di/ApiDiModule.cs b/Pds/Pds.Di/ApiDiModule.cs index 449448df..ee8b11a9 100644 --- a/Pds/Pds.Di/ApiDiModule.cs +++ b/Pds/Pds.Di/ApiDiModule.cs @@ -10,39 +10,28 @@ public class ApiDiModule : Module { protected override void Load(ContainerBuilder builder) { - Pds.Data.AssemblyRunner.Run(); - Pds.Services.AssemblyRunner.Run(); - var assemblies = AppDomain.CurrentDomain - .GetAssemblies() - .OrderByDescending(a => a.FullName) - .ToArray(); - - ServicesRegister(ref builder, assemblies); - RepositoriesRegister(ref builder, assemblies); - UnitOfWorkRegister(ref builder); - UnitOfWorkRegister(ref builder); + ServicesRegister(builder); + RepositoriesRegister(builder); + UnitOfWorkRegister(builder); } - private void ServicesRegister(ref ContainerBuilder builder, Assembly[] assemblies) + private void ServicesRegister(ContainerBuilder builder) { - var servicesAssembly = assemblies.FirstOrDefault(t => t.FullName.ToLower().Contains("pds.services,")); + var servicesAssembly = typeof(Services.AssemblyRunner).Assembly; builder.RegisterAssemblyTypes(servicesAssembly) .Where(t => t.Name.EndsWith("Service")) .AsImplementedInterfaces(); } - private void RepositoriesRegister(ref ContainerBuilder builder, Assembly[] assemblies) + private void RepositoriesRegister(ContainerBuilder builder) { - builder.RegisterGeneric(typeof(RepositoryBase<>)) - .As(typeof(IRepositoryBase<>)); - - var dataAssembly = assemblies.FirstOrDefault(t => t.FullName.ToLower().Contains("pds.data,")); + var dataAssembly = typeof(Pds.Data.AssemblyRunner).Assembly; builder.RegisterAssemblyTypes(dataAssembly) .Where(t => t.Name.EndsWith("Repository")) .AsImplementedInterfaces(); } - private void UnitOfWorkRegister(ref ContainerBuilder builder) + private void UnitOfWorkRegister(ContainerBuilder builder) { builder.RegisterType(typeof(UnitOfWork)) .As(typeof(IUnitOfWork)); diff --git a/Pds/Pds.Services/AssemblyRunner.cs b/Pds/Pds.Services/AssemblyRunner.cs index a2162587..4e436853 100644 --- a/Pds/Pds.Services/AssemblyRunner.cs +++ b/Pds/Pds.Services/AssemblyRunner.cs @@ -2,8 +2,4 @@ public static class AssemblyRunner { - public static void Run() - { - - } } \ No newline at end of file