Pergunta

Really strange issue with Automapper (3.1.1) randomly failing to map any data. The objects are created (ProductRating) and returned but none of the data is actually mapped. I've stepped into the code when this happens and the result variable does contain the data, but DynamicMap doesn't do its thing.

If I either, open a view and re-save it (no code changes) or if I re-compile the service were the below code lies, it just starts working again. Perhaps it's because I've nested a DynamicMap call in an anonymous type?

var result = _repo.Table.Where(a => a....)
    .Select(a => new
    {
        a.ProductRating.UserId,
        a.ProductRating.ProductId,
        a.ProductRating.Rating,
        a.ProductRating.CreatedOn,
        a.Product.Name
    }).ToList();

// automapper is failing to map any data here intermittently.
List<ProductRating> entityList = result.Select(a => Mapper.DynamicMap<ProductRating>(new
    {
        a.UserId,
        a.ProductId,
        a.Rating,
        a.CreatedOn,
        Product = Mapper.DynamicMap<Product>(new { a.ProductId, a.Name })
    })).ToList();

Update 1:

I ran a few tests below and it appears that the nested DynamicMap call is not the issue. I'm able to reproduce this issue consistently now provided I rebuild my solution and initially load a View like the home page then navigate to the View using this code then DynamicMap doesn't work. If I rebuild and initially load the View directly associated with this code then it works just fine. odd stuff.

var itemList = new List<ProductRating>();
result.ForEach(a =>
{
    // doesn't map anything
    itemList.Add(Mapper.DynamicMap<ProductRating>(new
    {
        a.UserId,
        a.ProductId,
        a.Rating,
        a.CreatedOn,
        // doesn't map either
        Product = new Product() { Name = a.Name, ProductId = a.ProductId }
        // doesn't map
        //Product = Mapper.DynamicMap<Product>(new { a.ProductId, a.Name })
    }));
    // success
    itemList.Add(new ProductRating()
    {
        UserId = a.UserId,
        ProductId = a.ProductId,
        Rating = a.Rating,
        CreatedOn = a.CreatedOn,
        Product = new Product() { Name = a.Name, ProductId = a.ProductId }
    });
});
Foi útil?

Solução

This issue continued to crop up and be the source of defects. With no response from the owner(s) of the project and after careful consideration of not only Automapper but also ValueInjector I decided to drop use of all object-to-object mappers and just manually new up instances of classes. The decision really comes down to not being at the mercy of third party software with no guarantee of a fix or response time.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top