Skip to content

Minimalistic library to extendMicrosoft.EntityFrameworkCore by supporting Repository and UnitOfWork

License

Notifications You must be signed in to change notification settings

moattarwork/UnitOfWork

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

79 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EfCore.UnitOfWork

Build status NuGet Status

A library for Microsoft.EntityFrameworkCore to support repository, unit of work patterns, and multiple database with transaction support. The project is an initial fork from https://github.com/Arch/UnitOfWork/ in order to make the library easier to use.

Doumentation

Full documentation is available here

Quickly start

Install the unit of work package from nuget.org.

dotnet add package EfCore.UnitOfWork
Install-Package EfCore.UnitOfWork

Configure relevant services in DI container and start using it:

public void ConfigureServices(IServiceCollection services)
{

    services
        .AddDbContext<QuickStartContext>(opt => opt.UseInMemoryDatabase())
        .AddUnitOfWork<QuickStartContext>();
}

public class ValuesController
{
    private readonly IUnitOfWork<QuickStartDbContext> _unitOfWork;

    public ValuesController(IUnitOfWork<QuickStartDbContext> unitOfWork) =>
        _unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));

    public async Task AddNewPostForNewUserAsync()
    {
        var user = new User {UserName = "sample-user", Password = "password"};
        _unitOfWork.GetRepository<User>().Insert(user);

        var post = new Post
        {
            UserId = user.UserId,
            Content = "Some comments",
            PublishDate = DateTime.Now,
            Tags = new List<PostTag> {new PostTag {Label = "Interesting"}, new PostTag {Label = "Social"}}
        };
        _unitOfWork.GetRepository<Post>().Insert(post);

        await _unitOfWork.SaveChangesAsync();
    }        
    
    public async Task<IEnumerable<Post>> LoadPostsAndRelevantTagsForGivenUser(int userId) =>
        await _unitOfWork.GetRepository<Post>()
            .GetListAsync(p => p.UserId == userId, q => q.OrderBy(m => m.PublishDate).Include(m => m.Tags));
}

About

Minimalistic library to extendMicrosoft.EntityFrameworkCore by supporting Repository and UnitOfWork

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%