分层架构:DTO和AutoMapper

AutoMapper

这些DTO需要和BLL里的entity进行映射。为了避免大量的手写代码,我们可以引入:

通过 nuget 添加引用AutoMapper(演示:略)

MapperConfiguration:映射的配置
IMapper:执行映射,依靠方法:Map()

在SRV的基类 BaseService 中引入静态构造函数,生成MapperConfiguration

    protected static MapperConfiguration autoMapperConfig;
    static BaseService()
    {
        autoMapperConfig = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap();
        });

if DEBUG //复习:这是什么?

        autoMapperConfig.AssertConfigurationIsValid();

endif

    }因为MapConfiguration的生成是比较耗费资源的,所以让其只在启动时生成一次(理解:为什么这样就能实现?)

然后,在 BaseService 中引入一个 mapper 属性,供所有子类使用:

    protected IMapper mapper
    {
        get
        {
            return autoMapperConfig.CreateMapper();
        }
    }mapper是“轻量级”的,一般不需要特别处理。(估计AutoMapper内部也进行了相应的处理,不是每次CreateMapper()就真的new一个)

最后,在SRV的子类中就可以调用:

        return mapper.Map(suggest);

//或者

        return mapper.Map(dtoSuggest);

//再或者:更新自己

        mapper.Map(dtoSuggest, suggest);

AutoMapper可以自动的映射名称相同的属性。如果名称不同,需要进行配置:

            cfg.CreateMap()
                .ForMember(s => s.Title, opt => opt.MapFrom(d => d.Subject))
            ;

还可以配置忽略某个属性:

                .ForMember(s => s.Title, opt => opt.Ignore())以及,如果某个属性为null值时的处理方案(AutoMapper不会报NullReferenceException)
                .ForMember(s => s.Age, opt => opt.NullSubstitute(-1))AutoMapper还可以自动映射整个集合。

更多的特性,留待同学们自己去发掘……o( ̄︶ ̄)o

复杂类型属性
包括集合元素

映射和逻辑
由于MapFrom()提供了强大的,疑惑与哪些逻辑写在MapFrom(),哪些写在Service方法中?

一般来说,AutoMapper中只需要执行一些简单的“映射”逻辑

名称不同:Entity中使用
Null值替换
多对多
不应该通过MapFrom()实现:

抽取文章摘要
关键字https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...

你可能感兴趣的:(前端)