1
私は6クラスを持っていると私は、これは私がマッピングするオブジェクトですAutoMapper4つの異なるプロパティクラスを持つ2つのオブジェクトをマップするにはどうすればよいですか?
public class Alert
{
public string MessageSender { get; set; }
public Site Site { get; set; }
public IEnumerable<Recipient> Recipients { get; set; }
}
public class AlertModel
{
public string Sender { get; set; }
public SiteModel Site { get; set; }
public IEnumerable<RecipientModel> Recipients { get; set; }
}
public class Site
{
public int Id { get; set; }
}
public class SiteModel
{
public int Id { get; set; }
}
public class Recipient
{
public int Id { get; set; }
public string CallId { get; set; }
}
public class RecipientModel
{
public int Id { get; set; }
public string CallId { get; set; }
}
private static void ConfigureAlertMapping()
{
AutoMapper.Mapper.Initialize(cfg =>
cfg.CreateMap<Alert, AlertModel>()
.ForMember(dest => dest.Sender, opt => opt.MapFrom(src => src.MessageSender))
);
}
private static void ConfigureRecipientMapping()
{
AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Recipient, RecipientModel>());
}
private static void ConfigureSiteMapping()
{
AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Site, SiteModel>());
}
でそれらをMAPPたいです。私はこれを呼ぶが、例外をスローしたい
Alert alert = new Alert()
{
Site = new Site() { Id = 1 },
Recipients = new List<Recipient> { new Recipient() { CallId = "1001" } }
};
... :(
AlertModel alertOutputInfo = AutoMapper.Mapper.Map<Alert, AlertModel>(alert);
これはエラーです:あなたは、別の解決策をしてください見つける場合
>>Mapping types:
Alert -> AlertModel
UniteAlerter.Domain.Models.Alert -> UniteAlerter.Gateway.Models.AlertModel
at lambda_method(Closure , Alert , AlertModel , ResolutionContext)
at AutoMapper.Mapper.AutoMapper.IMapper.Map[TSource,TDestination](TSource source)
at AutoMapper.Mapper.Map[TSource,TDestination](TSource source)
at UniteAlerter.Gateway.AlertGateway.<SendAlert>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
ここに追加してください。
マッピング設定も指定できますか – Shevek
確かに、最初のコードコメント(最後の3つの方法)を下にスクロールする必要があります。 – Alex
私はStartup-> ConfigureServices(IServiceCollectionサービス)で呼び出されたConfigureという名前のメソッドを呼び出します。 – Alex