5

依存関係のインストール中に IoC コンテナーを使用するのは悪い習慣ですか、それともコードの匂いですか?

これは私のコンポジションルートです:

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    Assembly modelAssembly = typeof(UserLoginModel).Assembly;
    Assembly controllerAssembly = typeof(HomeController).Assembly;

    container.Install(
        new MvcInfrastructureInstaller(modelAssembly, viewAssembly, controllerAssembly, applicationTitle, resourceAssemblyLocations),
        new MiniMembershipInstaller(),
        new ServiceInstaller(),
        new RepositoryInstaller(),
        new LibraryInstaller(),
        new AutoMapperProfileInstaller() // this installer needs to resolve dependencies such as repositories through the container itself, so it goes last.
    );
}

マッパーAutoMapperProfileInstallerを初期化するために、依存関係を含むプロファイルを解決する必要があります

public class AutoMapperProfileInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        Profile entityToViewModel = container.Resolve<EntityToViewModelProfile>();
        Profile[] profiles = new[] { entityToViewModel };

        Mapper.Initialize(config =>
        {
            config.ConstructServicesUsing(container.Resolve);

            foreach (Profile profile in profiles)
            {
                config.AddProfile(profile);
            }
        });
    }
}

AutoMapperこれは多くのレベルで間違っているように感じます。プロファイルを初期化するより良い方法は何でしょうか?

4

1 に答える 1

0

このアプローチの唯一の間違いは、IWindowsInstaller実装を手動で指定することです。リフレクションを使用してそれらを見つけActivator.CreateInstance、実装をインスタンス化します。

これにより、システム内の各パーツ/モジュールが独自の登録を担当する柔軟な構成アプローチが可能になります。

ただし、AutoMapper構成IMapperConfiguration(単一責任)用の新しいインターフェイスを作成します。リフレクションを使用して、アセンブリをスキャンします。

于 2012-08-14T06:46:59.543 に答える