首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >映射到两个不同的集合类型时引发AutoMapper异常

映射到两个不同的集合类型时引发AutoMapper异常
EN

Stack Overflow用户
提问于 2012-12-10 15:59:50
回答 1查看 865关注 0票数 0

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

代码语言:javascript
复制
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类。我包含了祖先类的类声明,因为我认为泛型约束的使用可能会有问题,但我不能完全确定。

代码语言:javascript
复制
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 User类中的AddressCollection和我的服务的UserModel类中的List<AddressModel>之间的差异(从AutoMapper的角度来看)。

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

代码语言:javascript
复制
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的属性。很明显我漏掉了什么。如有任何建议、建议或澄清,我们将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-11 05:29:07

我相信我已经找到了一个解决方案。经过大量的挖掘(以及一些值得称赞的地方),我发现我需要通过为用户地址集合创建一个类型转换器来定义映射:

代码语言:javascript
复制
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就可以让一切正常工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13797012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档