문제

I am trying to implement Unit of Work/Repository pattern in my MVC Web application.

Since DbContext itself is a unit of work, I want to mix it with my own UOW for testing and decoupling purposes (decoupling business layer from EF). Is it then a good idea to just wrap my DbContext inside an UOW class like the following?

Example:

Code reduced for clarity

public interface IUnitOfWork
{
    void Save();
}

public MyContext : DbContext
{
    // DbSets here
}

public UnitOfWork : IUnitOfWork
{
    public MyContext myContext { get; set; }

    void Save()
    {
        myContext.SaveChanges();
    }
}

Then I would call UnitOfWork instance to perform data operations.

Thanks a lot in advance :-)

도움이 되었습니까?

해결책

I depends on what you are trying to accomplish.

I you want to create an abstraction layer between Entity Framework and your business logic, then yes, it's a good idea. But then you have to do a complete abstraction meaning that your repository classes can not expose IQueryable<T>.

If you don't create a complete abstraction, then I do not see any reason to wrap the DbContext in a unit of work class.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top