0

我正在编写一个使用 AutoMapper 进行对象映射的 ASP.NET Web API 服务。我正在针对 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 属性,我不知道如何解决AddressCollectionAbleCommerceUser类和List<AddressModel>我的服务UserModel类之间的差异(从 AutoMapper 的角度来看)。

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

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. 我显然错过了一些东西。任何建议、建议或澄清将不胜感激。

4

1 回答 1

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类型。一旦我更改了源代码,它只需要一个简单的 LINQCast就可以让一切顺利进行。

于 2012-12-10T21:29:06.790 回答