私は AutoMapper の初心者です
次のドメインモデルがあります。
public class Contact
{
public string University { get; set; }
public string GraduationYear { get; set; }
}
ドメイン モデルを変更したくないので、GraduationYear プロパティに追加の属性を追加したいと考えています。
mvc 4 プロジェクトに属性クラスを作成しました
[System.AttributeUsage(System.AttributeTargets.Property)]
public class Mapping : System.Attribute
{
public string Name { get; set; }
}
「ContactMapping」という名前のクラスで上記の属性を使用
public class ContactMapping
{
[Telephony.Helper.Attribute.Mapping(Name = "Graduate")]
public string GraduationYear { get; set; }
}
カスタム属性「マッピング」を持たないドメイン サービスから連絡先オブジェクトを取得しています。
var contact = new ContactService().Get(contactPredicate).Single();
今、私はオートマッパーをやっています
AutoMapper.Mapper.CreateMap<Telephony.Helper.Mapping.ContactMapping, Aurora.CustomersMvc.Domain.Contact>();
var test = AutoMapper.Mapper.Map<Aurora.CustomersMvc.Domain.Contact>(contact);
var contactwithCustomAttr = contact.GetType().GetProperties().Where(p => ((Telephony.Helper.Attribute.Mapping[])p.GetCustomAttributes(typeof(Telephony.Helper.Attribute.Mapping), false))
.Any(attr => attr.ToString() == "Graduate"));
contactwithCustomAttr.Count() ゼロを取得しています。マッパーがカスタム プロパティを適用する必要があるため、GraduationYear プロパティを返すことを期待しています。