オブジェクト マッピングに 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
。私は明らかに何かが欠けています。提案、アドバイス、または説明をいただければ幸いです。