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 パッケージのUserおよびクラスです。Addressジェネリック制約の使用に問題がある可能性があると思われるため、先祖クラスのクラス宣言を含めましたが、完全にはわかりません。

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 クラスへのマッピングは機能します。AddressCollectionただし、Addresses プロパティの場合、(AutoMapper の観点から) AbleCommerceUserクラスの とList<AddressModel>サービスのクラスの の違いを解決する方法がわかりません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。私は明らかに何かが欠けています。提案、アドバイス、または説明をいただければ幸いです。

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です。ソースを変更したら、単純な LINQ だけでCastすべてがうまくいくようになりました。

于 2012-12-10T21:29:06.790 に答える