2016-03-21 6 views
3

私は既存のソリューションで作業しています。このソリューションはWindsor IoCを使用しています。私は、次のいた、私のGlobal.asax廃止された静的APIから離れるAutoMapper

public class AutoMapperMappings 
{ 
    public static void Configure() 
    { 
     AutoMapper.Mapper.Configuration 
     .CreateMap<LatestUpdateModel, LatestUpdate>(); 

     AutoMapper.Mapper.Configuration 
     .CreateMap<LatestUpdate, LatestUpdateModel>(); 

     AutoMapper.Mapper.Configuration 
     .CreateMap<DownloadLinkModel, DownloadLink>(); 

     AutoMapper.Mapper.Configuration 
     .CreateMap<DownloadLink, DownloadLinkModel>(); 

     AutoMapper.Mapper.Configuration 
     .CreateMap<NavigationElementModel, NavigationElement>(); 

     AutoMapper.Mapper.Configuration 
     .CreateMap<NavigationElement, NavigationElementModel>(); 

     AutoMapper.Mapper.Configuration 
     .CreateMap<Promobox, PromoboxModel>(); 

     AutoMapper.Mapper.Configuration 
     .CreateMap<PromoboxModel, Promobox>(); 
    } 
} 

protected void Application_Start(object sender, EventArgs e) 
{ 
    IoCContainer(); 
    ConfigureAutoMapperMappings(); 
} 

protected virtual void ConfigureAutoMapperMappings() 
{ 
    AutoMapperMappings.Configure(); 
} 

上記は私が静的APIから離れるべきだと言って警告を与えている私はこのようになりますAutomapperMappings.csクラスを持っています。だから私は周りGoogleで検索し、私はこれに私のAutomapperMappings.csを変更する提案し、いくつかの読書をした:

public class AutoMapperMappings 
{ 
    public static void Configure() 
    { 
     var config = new MapperConfiguration(cfg => 
     { 
      cfg.CreateMap<LatestUpdateModel, LatestUpdate>(); 
      cfg.CreateMap<LatestUpdate, LatestUpdateModel>(); 
      cfg.CreateMap<DownloadLinkModel, DownloadLink>(); 
      cfg.CreateMap<DownloadLink, DownloadLinkModel>(); 
      cfg.CreateMap<NavigationElementModel, NavigationElement>(); 
      cfg.CreateMap<NavigationElement, NavigationElementModel>(); 
      cfg.CreateMap<Promobox, PromoboxModel>(); 
      cfg.CreateMap<PromoboxModel, Promobox>(); 
     }); 
    } 
} 

すべての罰金ですが、変数var configは実際にはどこにも使用されていないので、私は、私はいくつかを行う必要があると確信していることより多くのものが、私は何を変える必要があるのか​​、どこでどこを知るのか分かりません。

+1

https://github.com/AutoMapper/AutoMapper/wiki/Migrating-from-static-API –

+0

Thanks @ VadimMartynov、StructureMapのおかげで、私の邪魔は混乱しています。私はそれを持っていないし、私はIoCのためにStructureMapを使用していない。 'IoCContainer'メソッド – Ciwan

答えて

5

チュートリアル"Migrating from static API"があります。

あなたはmapperオブジェクトを作成し、IoCコンテナにそれを登録する必要があります

public class AutoMapperMappings 
{ 
    public static void Configure(IWindsorContainer container) 
    { 
     var config = new MapperConfiguration(cfg => 
     { 
      cfg.CreateMap<LatestUpdateModel, LatestUpdate>(); 
      ... 
     }); 

     var mapper = config.CreateMapper(); 

     // register your mapper here. 
     container.Register(Component.For<IMapper>().Instance(mapper)); 
    } 
} 

今、あなたはエンティティをマップする必要があるクラスにごマッパーを注入することができます

public class ExampleClass 
{ 
    private readonly IMapper _mapper; 
    public ExampleClass(IMapper mapper) 
    { 
     _mapper = mapper; 
    } 

    public void DoWork() 
    { 
     var model = new LatestUpdateModel(); 
     ... 
     var update = mapper.Map<LatestUpdateModel, LatestUpdate>(model); 
    } 
} 

この移行意志があなたを助けますIMapperインターフェイスへのモックオブジェクトを作成することで、コードをよりテスト可能にすることができます。

+0

完全に作業しました。ありがとうございます。 – Ciwan

0

私はジミーBogards(Automapperクリエータ)をチェックアウトするだろうここに投稿:

https://lostechies.com/jimmybogard/2016/01/21/removing-the-static-api-from-automapper/

IMapper mapper = config.CreateMapper(); 

を設定は、新しいを作成し、テスト目的のために簡単に使用することができますIMappperインタフェースを返すために使用することができますマッピングなど。

+0

で複雑なものを見ることができないため、' IMapper mapper'を返すように 'Configure()'メソッドを変更しましたか?今すぐ 'void'です – Ciwan

+0

IMapperをクラスに注入するために使用できるIOC設定を作成してください。 –