1

I'm working on an ASP.NET MVC 3 application using AutoMapper 2.2.0. I have some AutoMapper profiles declared and when I initialize them manually everything works just fine.

AutoMapper.Mapper.Initialize(x =>
{
    x.AddProfile<ProdutoToProdutoViewModel>();                    
    x.AddProfile<IPagedListProdutoToIPagedListProdutoViewModel>();
    x.AddProfile<ItemToItemViewModel>();
    x.AddProfile<CarrinhoToCarrinhoViewModel>();
});
//This is working

But when I try to initialize them with Bootstrapper.AutoMapper 2.0.3.0...

Bootstrapper.With.AutoMapper().Start();

...a configuration exception is thrown:

The following property on eGuruShop.Web.ViewModels.ItemViewModel cannot be mapped:
Itens
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type eGuruShop.Web.ViewModels.ItemViewModel.
Context:
Mapping to property Itens from eGuruShop.Domain.CatalogoProdutos.Item to eGuruShop.Web.ViewModels.ItemViewModel
Mapping to property Itens from System.Collections.Generic.IList`1[[eGuruShop.Domain.CatalogoProdutos.Item, eGuruShop.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.IList`1[[eGuruShop.Web.ViewModels.ItemViewModel, eGuruShop.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Mapping from type eGuruShop.Domain.CatalogoProdutos.Carrinho to eGuruShop.Web.ViewModels.CarrinhoViewModel
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown

The CarrinhoToCarrinhoViewModel profile depends on the ItemToItemViewModel profile and when I change the initialization order to

AutoMapper.Mapper.Initialize(x =>
{
    x.AddProfile<ProdutoToProdutoViewModel>();                    
    x.AddProfile<IPagedListProdutoToIPagedListProdutoViewModel>();
    x.AddProfile<CarrinhoToCarrinhoViewModel>();
    x.AddProfile<ItemToItemViewModel>();
});
//Exception

I've got the same exception than before.

I suspect Bootstrapper is initializing the profiles in the wrong order, but I don't know how to solve it without abandoning Bootstrapper. Any suggestions or solutions to this problem?

Thanks

danseery
  • 486
  • 2
  • 11
peflorencio
  • 2,284
  • 2
  • 32
  • 40
  • Could you merge the profiles? (at least initially merge them into one to conclusively confirm that it really is the issue). – Mightymuke Jan 17 '13 at 09:55
  • In your first code snippet, where you do a manual initialization with profiles, you say it works -- but I don't see a call to Mapper.AssertConfigurationIsValid(). That's the only way to verify your mappings are correct. I wonder if Bootstrapper is doing this call and it really is an issue in your mapping. – PatrickSteele Jan 18 '13 at 14:51
  • @Mightymuke - Yes, I did it and the exception was gone. It seems that the order is really the problem. – peflorencio Jan 18 '13 at 19:27
  • @PatrickSteele - Mapper.AssertConfigurationIsValid() is being called in the Configure() method of each profile. It's the Mapper.AssertConfigurationIsValid() of CarrinhoToCarrinhoViewModel that's throwing the exception – peflorencio Jan 18 '13 at 19:34
  • 1
    I only do one call to AssertConfigurationIsValid -- once I've completed *all* initialization. You might want to try that and see if it makes a difference. – PatrickSteele Jan 19 '13 at 13:14

0 Answers0