1

ジミーボガートにAutomapper with an IoC containerの記事があります。彼はStructureMapの使用例を持っていますが、私はUnityを使用しています。InjectionConstructorを正しく使用する方法がわかりません。Unityでこれを行うにはどうすればいいですか?

以下は、私の貧弱な試みである以下の記事のコードです。誰も私にこのことを正しく行う方法を教えてもらえますか?

public class ConfigurationRegistry : Registry 
{ 
    public ConfigurationRegistry() 
    { 
     ForRequestedType<Configuration>() 
      .CacheBy(InstanceScope.Singleton) 
      .TheDefault.Is.OfConcreteType<Configuration>() 
      .CtorDependency<IEnumerable<IObjectMapper>>().Is(expr => expr.ConstructedBy(MapperRegistry.AllMappers)); 

     ForRequestedType<IConfigurationProvider>() 
      .TheDefault.Is.ConstructedBy(ctx => ctx.GetInstance<Configuration>()); 

     ForRequestedType<IConfiguration>() 
      .TheDefault.Is.ConstructedBy(ctx => ctx.GetInstance<Configuration>()); 
    } 
} 

私の試み:

container.RegisterType<IConfiguration, Configuration>(new SingletonLifetime()) 
        .Configure<InjectedMembers>() 
         .ConfigureInjectionFor<Configuration>(
          new InjectionConstructor(typeof(IEnumerable<IObjectMapper>)), MapperRegistry.AllMappers); 

答えて

1

これは私がやってしまったものです:

 IEnumerable<IObjectMapper> allMappers = new List<IObjectMapper>() { 
      new TypeMapMapper(TypeMapObjectMapperRegistry.AllMappers()), 
      new StringMapper(), 
      new FlagsEnumMapper(), 
      new EnumMapper(), 
      new ArrayMapper(), 
      new DictionaryMapper(), 
      new EnumerableMapper(), 
      new AssignableMapper(), 
      //new TypeConverterMapper(), 
      new NullableMapper(), 
     }; 

     container.RegisterType<Configuration>(new SingletonLifetime()) 
         .Configure<InjectedMembers>() 
          .ConfigureInjectionFor<Configuration>(
           new InjectionConstructor(allMappers)); 

    container.RegisterType<IConfigurationProvider, Configuration>(); 
    container.RegisterType<IConfiguration, Configuration>(); 
    container.RegisterType<IMappingEngine, MappingEngine>(); 

これは動作しますが、他の誰かがより良い実装を持っている場合、私はすべての耳だとこれにはまだ恩恵があります。

+0

Automapper MapperRegistry.AllMappers()静的メソッドを使用して、手動ではなくすべてのマッパーを取得します。しかし、私はSMで最初のステートメントを凝縮する以外は同様のセットアップを使用します。 –

+0

よく私の問題の一部です。設定用コンストラクタはIEnumerable を取りますが、MapperRegistry.AllMappers()静的メソッドはFunc > –

+0

を返します。次に、TypeMapObjectMapperRegistry.AllMappers()()を実行します。キーは、括弧の2番目のセットです。これは、 'AllMappers'から返されたデリゲートが呼び出され、' IEnumerable 'を返します。 –

関連する問題