Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve DI registration + Modify UnitOfWork to use Repos from DI #235

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Pds/Pds.Data/AssemblyRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@

public static class AssemblyRunner
{
public static void Run()
{

}
}
5 changes: 2 additions & 3 deletions Pds/Pds.Data/IUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Pds.Data;

public interface IUnitOfWork : IDisposable
public interface IUnitOfWork
{
IPersonRepository Persons { get; }
IResourceRepository Resources { get; }
Expand All @@ -15,7 +15,6 @@ public interface IUnitOfWork : IDisposable
IGiftRepository Gifts { get; }
ISettingRepository Settings { get; }
IDashboardRepository Dashboard { get; }

void Save();

EntityEntry GetContextEntry(object obj);
}
92 changes: 44 additions & 48 deletions Pds/Pds.Data/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IPersonRepository> personRepository;
private readonly Lazy<IResourceRepository> resourceRepository;
private readonly Lazy<IContentRepository> contentRepository;
private readonly Lazy<IBrandRepository> brandRepository;
private readonly Lazy<IClientRepository> clientRepository;
private readonly Lazy<IBillRepository> billRepository;
private readonly Lazy<ICostRepository> costRepository;
private readonly Lazy<IGiftRepository> giftRepository;
private readonly Lazy<ISettingRepository> settingRepository;
private readonly Lazy<IDashboardRepository> dashboardRepository;

public UnitOfWork(
ApplicationDbContext context,
Lazy<IPersonRepository> personRepository,
Lazy<IResourceRepository> resourceRepository,
Lazy<IContentRepository> contentRepository,
Lazy<IBrandRepository> brandRepository,
Lazy<IClientRepository> clientRepository,
Lazy<IBillRepository> billRepository,
Lazy<ICostRepository> costRepository,
Lazy<IGiftRepository> giftRepository,
Lazy<ISettingRepository> settingRepository,
Lazy<IDashboardRepository> 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;
}
}
27 changes: 8 additions & 19 deletions Pds/Pds.Di/ApiDiModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 0 additions & 4 deletions Pds/Pds.Services/AssemblyRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@

public static class AssemblyRunner
{
public static void Run()
{

}
}