关于前端:分层架构DTO和AutoMapper

27次阅读

共计 18197 个字符,预计需要花费 46 分钟才能阅读完成。

AutoMapper

这些 DTO 须要和 BLL 里的 entity 进行映射。为了防止大量的手写代码,咱们能够引入:

通过 nuget 增加援用 AutoMapper(演示:略)

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

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

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

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

        autoMapperConfig.AssertConfigurationIsValid();

endif

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

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

    protected IMapper mapper
    {
        get
        {return autoMapperConfig.CreateMapper();
        }
    }mapper 是“轻量级”的,个别不须要特地解决。(预计 AutoMapper 外部也进行了相应的解决,不是每次 CreateMapper()就真的 new 一个)

最初,在 SRV 的子类中就能够调用:

        return mapper.Map<Suggest, DTOSuggest>(suggest);

// 或者

        return mapper.Map<DTOSuggest, Suggest>(dtoSuggest);

// 再或者:更新本人

        mapper.Map<DTOSuggest, Suggest>(dtoSuggest, suggest);

AutoMapper 能够主动的映射名称雷同的属性。如果名称不同,须要进行配置:

            cfg.CreateMap<Suggest, DTOSuggest>()
                .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/…

正文完
 0