2012-08-06 6 views
5

종속성을 설치하는 동안 IoC 컨테이너를 사용하는 것이 좋지 않습니까?종속성을 설치하는 동안 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 프로파일을 초기화하는 더 좋은 방법은?

답변

0

유일한 접근법은 IWindowsInstaller 구현을 수동으로 지정한다는 것입니다. 리플렉션을 사용하여 해당 항목을 찾고 Activator.CreateInstance을 사용하여 구현을 인스턴스화합니다.

시스템의 각 부품/모듈이 자체 등록을 담당하는 유연한 구성 방식을 제공합니다.

그러나 AutoMapper 구성 IMapperConfiguration (단일 책임)에 대한 새 인터페이스를 만들 것입니다. 리플렉션을 사용하여 리플렉션을 사용하여 어셈블리도 스캔합니다.

관련 문제