How to ensure that Autofac calls Dispose () in an EF6 DbContext

UPDATE

Found this little stone that helped me with DbContext Josh Kodroff - creating the Entity Framework with the ability to test units

Original

After a lot of research, I decided to implement IOC using Autofac in my MVC5 EF6 project. The Autofac documentation was useful, but I'm still not sure if I need to call Dispose () either in my controller or in the service class?

I do not use abstract UOW and the Generic Repository, but just rely on the DbContext and DbSet <> provided in EF6. Here is a snippet of my classes.

My dbcontext

public class ProductContext : DbContext
{
    public ProductContext() : base("ProductContext")
    {
    }

    public DbSet<Availability> Availability { get; set; }       
    public DbSet<Category> Categories { get; set; } 
    ....
 }

My class of service

    public class ProductService : IProductService
{
    private ProductContext _db;
    public ProductService(ProductContext db)
    {
        _db = db;
    }

    public List<Product> GetProductsByCategory(string cleanCategory)
    {
        return _db.Products
             .Include(p => p.Options.Select(o => o.OptionGroup))
                 .Include(p => p.Associations.Select(a => a.AssociatedGroup))
                 .Include(p => p.Variations).Include(p => p.Manufacturer)
                 .Where(p => p.Active && p.Category.Name.ToUpper().Equals(cleanCategory)).ToList();
    }
    .....
}

My service interface

    public interface IProductService
{
    List<Product> GetProductsByCategory(string cleanCategory);
    ....
}

My controller

    public class ProductsController : Controller
{
    private IProductService _productService;
    public ProductsController(IProductService productService)
    {
        _productService = productService;
    }

    //GET: Products/
    public ActionResult Index(string category)
    {
        if (String.IsNullOrEmpty(category))
        {
            return HttpNotFound();
        }

        string cleanCategory = urlScrubber(category);
        var viewModel = new ProductsVM();
        viewModel.ProductList = _productService.GetProductsByCategory(cleanCategory);
}

My Autofac Container

var builder = new ContainerBuilder();

        // Register your MVC controllers.
        builder.RegisterControllers(typeof(MvcApplication).Assembly);

        // REGISTER COMPONENTS HERE:
        builder.RegisterType<ProductContext>().AsSelf().InstancePerRequest();
        builder.RegisterType<ProductService>().As<IProductService>().InstancePerRequest();

        // Set the dependency resolver to be Autofac.
        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

Dispose() , Autofac , IDisposable. ProductContext DbContext, Dispose(), .

-

builder.RegisterType<ProductContext>().As<DbContext>().InstancePerRequest();

, , Dispose?

builder.RegisterType<ProductContext>().AsSelf().InstancePerRequest();

, Autofac UOW DbContext, .

+4
2

Autofac . Autofacs ASP.NET MVC-, - , , , , - - .

, implments IDisposable, Dispose() . ,

builder.RegisterType<ProductContext>().As<DbContext>().InstancePerRequest();

.

+3

Autofac Func<> . , , :

builder.RegisterType<ProductContext>().As<IProductContext>();

ProductService:

public class ProductService : IProductService
{
    private IProductContext _dbCreator;
    public ProductService(Func<IProductContext> dbCreator)
    {
        _db = db;
    }

    public List<Product> GetProductsByCategory(string cleanCategory)
    {
        using (var dbCtx = _dbCreator())
        {

            return dbCtx.Products
                 .Include(p => p.Options.Select(o => o.OptionGroup))
                     .Include(p => p.Associations.Select(a => a.AssociatedGroup))
                     .Include(p => p.Variations).Include(p => p.Manufacturer)
                     .Where(p => p.Active && p.Category.Name.ToUpper().Equals(cleanCategory)).ToList();
        }
    }
    .....
}

, ProductService Func<> (_dbCreator), , , ProductContext autofac, , .

, , , IProductContext, , . ProductContext, Func<ProductContext> IProductContext, ..

builder.RegisterType<ProductContext>().AsSelf();

private ProductContext _dbCreator;
public ProductService(Func<ProductContext> dbCreator)

, , IDE... , , !

+2

Source: https://habr.com/ru/post/1611669/


All Articles