bev*_*qua 5 c# castle-windsor inversion-of-control automapper
在安装依赖项时使用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.
);
}
Run Code Online (Sandbox Code Playgroud)
我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);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
这在许多层面上都是错误的,初始化AutoMapper配置文件会有什么更好的方法?
该方法唯一的错误是您手动指定实现IWindowsInstaller。使用反射来查找它们并Activator.CreateInstance实例化实现。
它为您提供了一种灵活的配置方法,系统中的每个部分/模块都负责其自己的注册。
不过,我会为 AutoMapper 配置创建一个新界面IMapperConfiguration(单一职责)。也使用反射来扫描程序集以查找这些内容。
| 归档时间: |
|
| 查看次数: |
351 次 |
| 最近记录: |