2017-06-15 9 views
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() 

ここに追加してください。

+0

マッピング設定も指定できますか – Shevek

+0

確かに、最初のコードコメント(最後の3つの方法)を下にスクロールする必要があります。 – Alex

+0

私はStartup-> ConfigureServices(IServiceCollectionサービス)で呼び出されたConfigureという名前のメソッドを呼び出します。 – Alex

答えて

0

UPDATE(解決法)

問題が見つかりました...マッピングするすべてのクラスのマッパーを初期化しますが、解決策はすべてのマッピングでマッパーを1回初期化することです。

AutoMapper.Mapper.Initialize(
cfg => 
    { 
     cfg.CreateMap<Alert, AlertModel>().ForMember(dest => dest.Sender, opt => opt.MapFrom(src => src.MessageSender)); 
     cfg.CreateMap<Recipient, RecipientModel>(); 
     cfg.CreateMap<Site, SiteModel>(); 
     } 
); 
関連する問題