A plugin for Microsoft.EntityFrameworkCore to support repository, unit of work patterns, and multiple database with distributed transaction supported.
Install-Package UnitOfWork.EntityFrameworkCore -Version 5.3.1 (.NET5)
or
Install-Package UnitOfWork.EntityFrameworkCore -Version 6.0.3 (.NET6)
or
Install-Package UnitOfWork.EntityFrameworkCore -Version 7.0.2 (.NET7)
The following code demonstrates basic usage.
First of all, please register the dependencies into the MS Built-In container:
services.AddDbContext<ExampleDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString(SystemConstants.MainConnectionString), sqlServerOptions =>
{
sqlServerOptions.MigrationsAssembly((typeof(ExampleDbContext).Assembly).GetName().Name);
});
}).AddUnitOfWork<ExampleDbContext>();
(For .Net6,.Net7)
builder.Services.AddDbContext<BaseSourceDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString(SystemConstants.MainConnectionString)))
.AddUnitOfWork<BaseSourceDbContext>();
After that, use the structure in your code like that:
private readonly IUnitOfWork _unitOfWork;
// Injection
public ExampleController(IUnitOfWork<ExampleDbContext> unitOfWork )
{
_unitOfWork = unitOfWork;
}
#returns GetAll
public async Task GetAll(){
var books= _unitOfWork.GetRepository<Book>().GetAll();
}
public async Task GetAllByFilter(){
var blogs = _unitOfWork.GetRepository<Book>().GetAllByFilter(
predicate: x => x.Descriptions.Contains("Book Example"),
orderBy: x => x.OrderByDescending(i => i.DateCreated),
include: x => x.Include(i => i.Category));
}
public async Task GetAllByFilter(){
var blogs = _unitOfWork.GetRepository<Blog>().GetPagedListAsync(
predicate: x => x.Descriptions.Contains("Book Example"),
orderBy: x => x.OrderByDescending(i => i.DateCreated),
include: x => x.Include(i => i.Category),
pageIndex:1,pageSize:20);
}
public async Task GetStagesPaging(){
var repository = _unitOfWork.GetRepository<Book>();
var query = repository.Queryable()
.Where(x => x.Descriptions.Contains("Book Example"));
var books= _unitOfWork.GetRepository<Book>().GetStagesPagedListAsync(
stages: query,
selector:x=>x.Descriptions,
pageIndex: 1, pageSize: 20);
}
public async Task voidGetBookById(int id){
var book= await _unitOfWork.GetRepository<Book>().FindAsync(id);
}
public async Task AddBook(Book book){
await _unitOfWork.GetRepository<Book>().InsertAsync(book);
await _unitOfWork.SaveChangesAsync();
}
public async Task UpdateBook(Book book){
_unitOfWork.GetRepository<Book>().Update(book);
await _unitOfWork.SaveChangesAsync();
}
public async Task DeleteBook(int id){
_unitOfWork.GetRepository<Book>().Delete(id);
await _unitOfWork.SaveChangesAsync();
}
public async Task GetBookCount(){
var count= _unitOfWork.GetRepository<Book>().CountAsync(id);
}
......................
The operations above are also available as async.
If you want to help with the project, feel free to open pull requests and submit issues.