-
Notifications
You must be signed in to change notification settings - Fork 256
Description
Hi, I have a proposal for the way this library implements IDbContextFactory.
Currently, I believe this is the implementation: Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/ContextFactoryRepositoryBaseOfT.cs
I'm forced to use this pattern due to Blazor's Interactive Server-side scope problems.
The current way, I believe is not the best as in a mediator handler, you may have multiple db operations, so you want to keep the db context open throughout the lifetime of the handler, rather than only for singular db operations and make use of .SaveChanges().
My proposal is something akin to having a factory which contains the IDbContextFactory and has a function .CreateIRepository(), similar to .CreateDbContext().
Example would be ...
public class RepositoryFactory : IRepositoryFactory
{
private IDbContextFactory _dbContextFactory;
public RepositoryFactory(IDbContextFactory dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}
public Task IRepository CreateRepository()
{
return Task.FromResult(new IRepository(_dbContextFactory.CreateRepository()));
}
public void Dispose()
{
...
}
}
public class MyHandler(IRepositoryFactory repFactory)
{
public void Handle()
{
using IRepository<MyContext> repository = await IRepositoryFactory.CreateRepository();
...
}
}
It's not a fully fleshed out idea, but I was wondering if you think that's possible and would be open to it?