【发布时间】:2018-03-06 05:47:34
【问题描述】:
我有 WCF 服务,其作用类似于 ORM。我需要一个映射器将实体映射到 Dtos。考虑到关注点分离、SOLID和DDD,我想知道AutoMapper配置和Profiles应该去哪里?我的项目结构是这样的:
数据 (EF) -> 逻辑 -> WCF 服务/WCF 合同 -> WindowsService(主机)
我遵循 github 的规则,创建了一个 Ninject 模块:
public class AutoMapperModule : NinjectModule
{
public override void Load()
{
Bind<IValueResolver<SourceEntity, DestModel, bool>>().To<MyResolver>();
var mapperConfiguration = CreateConfiguration();
Bind<MapperConfiguration>().ToConstant(mapperConfiguration).InSingletonScope();
// This teaches Ninject how to create automapper instances say if for instance
// MyResolver has a constructor with a parameter that needs to be injected
Bind<IMapper>().ToMethod(ctx =>
new Mapper(mapperConfiguration, type => ctx.Kernel.Get(type)));
}
private MapperConfiguration CreateConfiguration()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfiles(new SampleProfile());
});
return config;
}
}
public class SampleProfile : Profile
{
public SomeProfile()
{
CreateMap<Foo, FooDto>();
}
}
我总是在可执行程序的入口处创建一个Ninject模块,所以在Windows Service中。但我有点困惑,因为我的 Windows 服务项目没有“数据”引用(带有实体)。所以我有几个问题:
- 是否应该在 WindowsService(主机)中添加对“Data”项目的引用并在 WindowsService 中创建 AutoMapper Profile 类?
- 是否应该在 Logic 类(知道 Dtos 和 Entities)中定义 AutoMapper Profile 类,然后在将初始化模块的 WindowsService 中引用 Logic?
- 我应该将 AutoMapper 配置文件放在其他地方吗?
谢谢!
【问题讨论】:
-
当然是基础设施。任何执行非常特定部分的第 3 部分库都是基础架构,包括但不限于:ORM、数据库提供程序、UI(MVC、WebForms)、端点(WCF、WebAPI)、身份验证、Xslt 处理器、服务/消息总线(实现)、本地和分布式缓存、Swagger,当然还有 AutoMapper。另外请记住,IoC 容器(因为您提到使用 ninject 模式)也是基础架构,因为它们是可替换的,而不是您的域/业务逻辑的重要组成部分
标签: c# wcf domain-driven-design automapper ninject