在安装依赖项时使用 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
配置文件的更好方法是什么?