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
?