2011-06-21 11 views
5

依存性注入を使用するカスタムリゾルバを試してみるとAutomapperに問題があります。AutomapperとWindsorの問題

私は次のモデルがあります:私は、次のコードを使用してマッピングするために

public class RegistrationViewModel 
{ 
    [Required]   
    public string Name { get; set; } 

    public int CountryId { get; set; } 

    public IEnumerable<Country> Countries { get; set; } 
} 

public class User : Entity 
{ 
    public virtual string Name { get; set; } 
    public virtual Country Country { get; set; } 
} 

public class Country : Entity 
{ 
    public virtual string Name { get; set; } 
} 

と、次のビューモデルを

以前
Mapper.Map(registrationViewModel, user); 

私が登録以下:

Mapper.Reset(); 
container = new WindsorContainer(); 
container.AddFacility<FactorySupportFacility>(); 
container.Register(Component.For<ISession>(). 
           UsingFactoryMethod(() => NHibernateSessionFactory.RetrieveSession()). 
           LifeStyle.Is(LifestyleType.Transient)); 
container.Register(Component.For(typeof(LoadingEntityResolver<>)).ImplementedBy(typeof(LoadingEntityResolver<>)).LifeStyle.Transient); 
Mapper.Initialize(x => 
     { 
      x.AddProfile<BasicProfile>(); 
      x.ConstructServicesUsing(container.Resolve); 
     }); 

私BasicProfileは以下の通りです:

カスタムリゾルバは、以下の方法で行われ
public class BasicProfile : Profile 
{ 
    public const string VIEW_MODEL = "MyBasicProfile"; 

    public override string ProfileName 
    { 
     get { return VIEW_MODEL; } 
    } 

    protected override void Configure() 
    { 
     CreateMaps(); 
    } 

    private void CreateMaps() 
    { 
     CreateMap<RegistrationViewModel, User>() 
      .ForMember(dest => dest.Country, _ => _.ResolveUsing<LoadingEntityResolver<Country>>().FromMember(src => src.CountryId)) 
      ); 

    } 
} 

public class LoadingEntityResolver<TEntity> : ValueResolver<int, TEntity> 
    where TEntity: Entity 
{ 
    private readonly ISession _session; 

    public LoadingEntityResolver(ISession session) 
    { 
     _session = session; 
    } 

    protected override TEntity ResolveCore(int source) 
    { 
     return _session.Load<TEntity>(source); 
    } 

} 

マッピングコードが実行されているI次の例外を取得:

AutoMapper.AutoMapperMappingException:ViewModels.RegistrationViewModelをModels.Userにマップしようとしています。 ViewModels.RegistrationViewModelからModels.Userへのマッピング構成の使用 'AutoMapper.AutoMapperMappingException'タイプの例外がスローされました。 ----> AutoMapper.AutoMapperMappingException:ViewModels.RegistrationViewModelをLModels.Countryにマップしようとしています。 ViewModels.RegistrationViewModelからModels.Userへのマッピング構成の使用 宛先プロパティ:国 'AutoMapper.AutoMapperMappingException'型の例外がスローされました。 ----> System.ArgumentExceptionの:タイプは 'Mapping.LoadingEntityResolver`1 [Models.Country]' 私が間違っているかもしれないものは考えているデフォルトコンストラクタ

を持っていません。それはおそらくリゾルバを構築しているものです。私が次のことを試しても問題はありません:

var resolver = container.Resolve<LoadingEntityResolver<Country>>(); 

Assert.IsInstanceOf<LoadingEntityResolver<Country>>(resolver); 

私はどんな助けでも素晴らしいでしょう。

敬具
ルカシュ

答えて

4

あなたは、私がデータベースまたは何からAutoMapperの解決実体を持つ避けるだろう:-)ここで起こってかなり多額のDIのものを持っています。コードを理解しにくくし、オブジェクトの寿命が悪くなる可能性があります。とにかく

、単に(間違った)から順番を入れ替え、あなたの問題を解決するには:(正しい)に

Mapper.Initialize(x => 
{ 
    x.AddProfile<BasicProfile>(); 
    x.ConstructServicesUsing(container.Resolve); 
}); 

Mapper.Initialize(x => 
{ 
    x.ConstructServicesUsing(container.Resolve); 
    x.AddProfile<BasicProfile>(); 
}); 
+0

はあなたの助けのためにありがとうございました。この単純な変更が私の問題を解決しました。私が使っているアプローチが良くないと思うなら、それでは何をお勧めしますか? – GUZ

関連する問題