映射两种不同的集合类型时,AutoMapper抛出异常

时间:2012-12-10 07:59:50

标签: asp.net-web-api automapper-2 ablecommerce

我正在编写一个ASP.NET Web API服务,它使用AutoMapper进行对象映射。我正在针对AbleCommerce购物车软件构建此服务。问题所在的服务(目的地)类跟踪用户及其地址。

public class UserModel
{
    public int UserID { get; set; }
    ...
    public List<AddressModel> Addresses { get; set; }
}

public class AddressModel 
{
    public int AddressID { get; set; }
    public int UserID { get; set; }
    ...
    // Contains some of the fields from AbleCommerce Address
}

我的来源是AbleCommerce包中的UserAddress类。我已经包含了祖先类的类声明,因为我相信使用泛型约束可能存在问题,但我不完全确定。

public class User: IPersistable
{
    public int UserId { get; set; }
    ...
    public AddressCollection Addresses 
    {
        get
        {
            // Calls an internal method which creates a new instance
            // of AddressCollection and loads the addresses for the
            // user from the database.
            ...
            return _Addresses;
        }
    }
}

public class Address : IPersistable 
{ 
    public int AddressId { get; set; }
    public int UserId { get; set; }
    ... 
}

public class AddressCollection : PersistentCollection<Address> { ... }

public class PersistentCollection<T> : SortableCollection<T> where T : IPersistable { ... }

public interface IPersistable { ... }

从AbleCommerce类到我的服务类的映射按预期工作。只要我忽略映射配置中不需要的属性,从我的服务类映射到AbleCommerce类就可以了。但是,对于Addresses属性,我不知道如何解决AbleCommerce AddressCollection类中的UserList<AddressModel>类之间的差异(从AutoMapper角度来看)我服务的UserModel课程。

当我测试映射配置时,我得到的异常是:

AutoMapper.AutoMapperConfigurationException : The following property on AlfredModel.Models.Classes.AddressModel cannot be mapped: 
    Addresses
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type AlfredModel.Models.Classes.AddressModel.
Context:
    Mapping to property Addresses from System.Object to AlfredModel.Models.Classes.AddressModel
    Mapping to property Addresses from CommerceBuilder.Users.AddressCollection to System.Collections.Generic.List`1[[AlfredModel.Models.Classes.AddressModel, AlfredModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
    Mapping to property User from CommerceBuilder.Users.User to AlfredModel.Models.Classes.UserModel
    Mapping from type CommerceBuilder.Orders.Basket to AlfredModel.Models.Classes.BasketModel
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.
   at AutoMapper.ConfigurationStore.DryRunTypeMap(ICollection`1 typeMapsChecked, ResolutionContext context)
   at AutoMapper.ConfigurationStore.DryRunTypeMap(ICollection`1 typeMapsChecked, ResolutionContext context)
   at AutoMapper.ConfigurationStore.DryRunTypeMap(ICollection`1 typeMapsChecked, ResolutionContext context)
   at AutoMapper.ConfigurationStore.DryRunTypeMap(ICollection`1 typeMapsChecked, ResolutionContext context)
   at AutoMapper.ConfigurationStore.AssertConfigurationIsValid(IEnumerable`1 typeMaps)
   at AutoMapper.ConfigurationStore.AssertConfigurationIsValid()
   at AutoMapper.Mapper.AssertConfigurationIsValid()
   at AlfredModelTests.AlfredMapConfigurationTest.Maps_Configured_Correctly() in C:\Users\ntruick\Documents\Visual Studio 2010\Projects\AlfredModel\AlfredModelTests\AlfredMapConfigurationTest.cs:line 21

这让我感到困惑,因为AddressModel类没有名为Addresses的属性。我显然遗漏了一些东西。任何建议,建议或澄清将不胜感激。

1 个答案:

答案 0 :(得分:0)

我相信我已经找到了解决方案。经过多次挖掘(以及一些赞赏之后),我发现我需要通过为用户地址集创建类型转换器来自定义映射:

public class UserAddressesTypeConverter : 
    ITypeConverter<PersistentCollection<Address>, List<AddressModel>
{
    var source = (PersistentCollection<Address>)context.SourceValue;
    var result = new List<AddressModel>();

    foreach (Address address in source.Cast<Address>().ToList()
        result.Add(Mapper.Map<AddressModel>(address));

    return result;
}

我的困惑是基于我试图直接使用AddressCollection类来完成与此类似的事实。似乎PersistentCollection上的泛型约束不允许后代类正确地解析为Address类型。一旦我改变了源代码,它只需要一个简单的LINQ Cast来使一切都运转起来。