Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit of Work, Entity Framework DBContext Scope

I've run into a bit of a problem with EF looking for the best practice for this problem:

public void TestEntityFramework_UOWImplementation()
{
    using (UnitOfWorkInventory uow = new UnitOfWorkInventory())
    {
        IMaterialRepository repos = new MaterialRepository(uow);

        Material mat = GetMaterial("Mikes Material", 1);

        mat.CostPrice = 20;

        repos.InsertOrUpdate(mat);

        uow.Commit();
    }
}

private Material GetMaterial(string sku, int clientId)
{
    IMaterialRepository repos = new MaterialRepository(new UnitOfWorkInventory();

    return repos.Find(sku, clientId);

}

In the TestEntityFramework_UOWImplementation() method, its fine, i call create a scope for my unit of work.. and create a repository inside it.

But when i want to getMaterials() as below.. I have no access to the unit of work or the repository, unless i actually pass it as a parameter! This is clearly not particularly nice.

How do people get around this problem??

Thanks in advance!

Neil

like image 800
Neil Hosey Avatar asked Jan 11 '13 10:01

Neil Hosey


People also ask

Is DbContext a unit of work?

The Entity Framework DbContext class is based on the Unit of Work and Repository patterns and can be used directly from your code, such as from an ASP.NET Core MVC controller. The Unit of Work and Repository patterns result in the simplest code, as in the CRUD catalog microservice in eShopOnContainers.

Is DbContext scoped?

This example registers a DbContext subclass called ApplicationDbContext as a scoped service in the ASP.NET Core application service provider (a.k.a. the dependency injection container). The context is configured to use the SQL Server database provider and will read the connection string from ASP.NET Core configuration.

Should DbContext be scoped or singleton?

Popular Answer. DbContext should not be used as a singleton because it is holding a connection object which cannot be used by multiple threads at the same time. You will run into errors if two requests try to use it at the same time. If your service depends on the context, the service cannot be a singleton.

What is unit of work in Entity Framework?

Unit of Work is the concept related to the effective implementation of the repository pattern. non-generic repository pattern, generic repository pattern. Unit of Work is referred to as a single transaction that involves multiple operations of insert/update/delete and so on.


1 Answers

In your implementation you wont have access to the Unit of Work like that. What I do is use an IoC container and Dependency Injection to handle it. I have a WCF service that uses Unit of Work with a repository pattern against EF5.

You can read more about repository pattern, unit of work, and EF here but basically what I do is in the constructor of my service class I inject the Unit of Work like so:

    private readonly IUnitOfWork uow;

    public LoanService(IUnitOfWork unitOfWork)
    {
        uow = unitOfWork;
    }

Then I can use uow.WhateverMethod in my repos anywhere in the service. I use Ninject to handle the injection of IUnitOfWork. Hope it helps you.

like image 160
Juan Avatar answered Nov 09 '22 21:11

Juan