Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper + EF4 + ASP.NET MVC - getting 'context disposed' error (I know why, but how to fix it?)

I have this really basic code in a MVC controller action. It maps an Operation model class to a very basic OperationVM view-model class .

public class OperationVM: Operation 
{
    public CategoryVM CategoryVM { get; set; }
}

I need to load the complete list of categories in order to create a CategoryVM instance.
Here's how I (try to) create a List<OperationVM> to show in the view.

public class OperationsController : Controller {

    private SomeContext context = new SomeContext ();

    public ViewResult Index()
    {
        var ops = context.Operations.Include("blah...").ToList();
        Mapper.CreateMap<Operation, OperationVM>()
            .ForMember(
                dest => dest.CategoryVM, 
                opt => opt.MapFrom(
                    src => CreateCatVM(src.Category, context.Categories)
                    //  trouble here ----------------^^^^^^^
                )
            );
        var opVMs = ops.Select(op => Mapper.Map<Operation, OperationVM>(op))
                       .ToList();

        return View(opVMs);
    }
}

All works great first time I hit the page. The problem is, the mapper object is static. So when calling Mapper.CreateMap(), the instance of the current DbContext is saved in the closure given to CreateMap().

The 2nd time I hit the page, the static map is already in place, still using the reference to the initial, now disposed, DbContext.

The exact error is:

The operation cannot be completed because the DbContext has been disposed.

The question is: How can I make AutoMapper always use the current context instead of the initial one?

Is there a way to use an "instance" of automapper instead of the static Mapper class? If this is possible, is it recommended to re-create the mapping every time? I'm worried about reflection slow-downs.

I read a bit about custom resolvers, but I get a similar problem - How do I get the custom resolver to use the current context?

like image 713
Cristian Diaconescu Avatar asked Aug 01 '12 01:08

Cristian Diaconescu


2 Answers

It is possible, but the setup is a bit complicated. I use this in my projects with help of Ninject for dependency injection.

AutoMapper has concept of TypeConverters. Converters provide a way to implement complex operations required to convert certain types in a separate class. If converting Category to CategoryVM requires a database lookup you can implement that logic in custom TypeConverter class similar to this:

using System;
using AutoMapper;

public class CategoryToCategoryVMConverter : 
        TypeConverter<Category, CategoryVM>
{
    public CategoryToCategoryVMConverter(DbContext context)
    {
        this.Context = context;
    }

    private DbContext Context { get; set; }

    protected override CategoryVM ConvertCore(Category source)
    {
        // use this.Context to lookup whatever you need
        return CreateCatVM(source, this.Context.Categories);
    }
}

You then to configure AutoMapper to use your converter:

Mapper.CreateMap<Category, CategoryVM>().ConvertUsing<CategoryToCategoryVMConverter>();

Here comes the tricky part. AutoMapper will need to create a new instance of our converter every time you map values, and it will need to provide DbContext instance for constructor. In my projects I use Ninject for dependency injection, and it is configured to use the same instance of DbContext while processing a request. This way the same instance of DbContext is injected both in your controller and in your AutoMapper converter. The trivial Ninject configuration would look like this:

Bind<DbContext>().To<SomeContext>().InRequestScope();

You can of course use some sort of factory pattern to get instance of DbContext instead of injecting it in constructors.

Let me know if you have any questions.

like image 168
LeffeBrune Avatar answered Oct 15 '22 16:10

LeffeBrune


I've found a workaround that's not completely hacky. Basically, I tell AutoMapper to ignore the tricky field and I update it myself.

The updated controller looks like this:

public class OperationsController : Controller {

    private SomeContext context = new SomeContext ();

    public ViewResult Index()
    {
        var ops = context.Operations.Include("blah...").ToList();
        Mapper.CreateMap<Operation, OperationVM>()
            .ForMember(dest => dest.CategoryVM, opt => opt.Ignore());

        var opVMs = ops.Select(
            op => {
                var opVM = Mapper.Map<Operation, OperationVM>(op);
                opVM.CategoryVM = CreateCatVM(op.Category, context.Categories);
                return opVM;
            })
            .ToList();

        return View(opVMs);
    }
}

Still curious how this could be done from within AutoMapper...

like image 37
Cristian Diaconescu Avatar answered Oct 15 '22 17:10

Cristian Diaconescu