Most of the repository pattern libraries for .NET or .NET Core depend on the Entity Framework.
Unlike all these frameworks, the SI.UnitOfWork has no such dependencies and follows the strict paradigms of the repository pattern.
But if you want, SI.UnitOfWork.EntityFrameworkCore provides a complete and ready-to-use library for SqLite, PostgreSQL, SQL Server and the InMemory provider of Entity Framework.
Install-Package SI.UnitOfWorkpublic void ConfigureServices(IServiceCollection services)
{
// ..
services.AddDbContext<IDbContext, SampleContext>(o => o.UseInMemoryDatabase("mem-db"));
services.AddUnitOfWork<SampleContext>();
services.AddScoped(typeof(IRepository<>), typeof(GenericRepository<>));
services.AddScoped(typeof(ICustomRepository), typeof(CustomRepository));
// ..
}public class SampleClass
{
// Opt. 1. Inject a UoW for regular DB read and write operations
// Opt. 2. Inject a UoW-Factory in case you work with multiple databases
// Opt. 3. Inject a RepositoryFactory to get read-only repositories
public SampleClass(
IUnitOfWork unitOfWork,
IUnitOfWorkFactory<SampleContext> unitofWorkFactory,
IRepositoryFactory<SampleContext> repositoryFactory)
{
IRepository<Person> personRepository; // Register generic repositories manually!
ICustomRepository customRepository; // Register custom repositories manually!
// Opt. 1
personRepository = unitOfWork.GetRepository<Person>();
customRepository = unitOfWork.GetRepository<Person, ICustomRepository>();
// .. do work
unitOfWork.SaveChanges();
// Opt. 2
using var uow = unitofWorkFactory.GetUnitOfWork();
personRepository = uow.GetRepository<Person>();
customRepository = uow.GetRepository<Person, ICustomRepository>();
// .. do work
uow.SaveChanges();
// Opt. 3
personRepository = repositoryFactory.GetRepository<Person>();
customRepository = repositoryFactory.GetRepository<Person, ICustomRepository>();
// No SaveChanges available!
}
}Install-Package SI.UnitOfWork.EntityFrameworkCorepublic void ConfigureServices(IServiceCollection services)
{
// ..
services.AddDbContext<EFContext>(o => o.UseInMemoryDatabase("mem-db");
services.AddEFUnitOfWork();
services.AddScoped(typeof(ICustomRepository), typeof(CustomRepository));
// ..
}public class SampleClass
{
public SampleClass(IUnitOfWork unitOfWork)
{
IRepository<Person> personRepository; // Generic repositories are automatically registered!
ICustomRepository customRepository; // Register custom repositories manually!
personRepository = unitOfWork.GetRepository<Person>();
customRepository = unitOfWork.GetRepository<Person, ICustomRepository>();
// .. do work
unitOfWork.SaveChanges();
}
}- Icon made by Freepik