zoukankan      html  css  js  c++  java
  • 惯例映射

    Conditional Object Mapper

    Conditional Object Mappers make new type maps based on conditional between the source and the destination type.

    var config = new MapperConfiguration(cfg => {
        cfg.AddConditionalObjectMapper().Where((s, d) => s.Name == d.Name + "Dto");
    });
    

    Member Configuration

    Member Configuration is like Configuration but you can have complete control on what is and isn't used.

    var config = new MapperConfiguration(cfg => { cfg.AddMemberConfiguration(); });
    

    AddMemberConfiguration() starts off with a blank slate. Everything that applies in Configuration by default is gone to start with.

    Naming Conventions

    AddMemberConfiguration().AddMember<NameSplitMember>() gets you default naming convention functionality.

    Can overwrite the source and destination member naming conventions by passing a lambda through the parameter.
    SourceExtentionMethods can also be set here.

    If you don't set anything AutoMapper will use DefaultMember, which will only check using the Name of the property.

    PS: If you don't set this Flattening of objects will be disabled.

    Replacing characters

    AddMemberConfiguration().AddName<ReplaceName>(_ => _.AddReplace("Ä", "A").AddReplace("í", "i"));

    Recognizing pre/postfixes

    AddMemberConfiguration().AddName<PrePostfixName>(_ => _.AddStrings(p => p.Prefixes, "Get", "get").AddStrings(p => p.DestinationPostfixes, "Set"));

    Attribute Support

    AddMemberConfiguration().AddName<SourceToDestinationNameMapperAttributesMember>(); *Currently is always on

    Looks for instances of SourceToDestinationMapperAttribute for Properties/Fields and calls user defined isMatch function to find member matches.

    MapToAttribute is one of them which will match the property based on name provided.

    public class Foo
    {
        [MapTo("SourceOfBar")]
        public int Bar { get; set; }
    }
    

    Getting AutoMapper Defaults

    AddMemberConfiguration().AddMember<NameSplitMember>().AddName<PrePostfixName>(_ => _.AddStrings(p => p.Prefixes, "Get"))

    Is the default values set by Configuration if you don't use AddMemberConfiguration().

    Expand-ability

    Each of the AddName and AddMember types are based on an interface ISourceToDestinationNameMapper and IChildMemberConfiguration. You can make your own classes off the interface and configure their properties through the lambda statement argument, so you can have fine tune control over how AutoMapper resolves property maps.

    Multiple Configurations

    Each configuration is its own set of rules that all must pass in order to say a property is mapped. If you make multiple configurations they are completely separate from one another.

    Profiles

    These can be added to Profile as well as the ConfigurationStore.

    Each Profiles rules are separate from one another, and won't share any conditions.
    If a map is generated from AddConditionalObjectMapper of one profile, the AddMemberConfigurations of only that profile can be used to resolve the property maps.

    Example

    Shown below is two profiles for making conventions for transferring to and from a Data Transfer Object.
    Each one is isolated to one way of the mappings, and the rules are explicitly stated for each side.

            // Flattens with NameSplitMember
            // Only applies to types that have same name with destination ending with Dto
            // Only applies Dto post fixes to the source properties
            public class ToDTO : Profile
            {
                protected override void Configure()
                {
                    AddMemberConfiguration().AddMember<NameSplitMember>().AddName<PrePostfixName>(
                            _ => _.AddStrings(p => p.Postfixes, "Dto"));
                    AddConditionalObjectMapper().Where((s, d) => s.Name == d.Name + "Dto");
                }
            }
            // Doesn't Flatten Objects
            // Only applies to types that have same name with source ending with Dto
            // Only applies Dto post fixes to the destination properties
            public class FromDTO : Profile
            {
                protected override void Configure()
                {
                    AddMemberConfiguration().AddName<PrePostfixName>(
                            _ => _.AddStrings(p => p.DestinationPostfixes, "Dto"));
                    AddConditionalObjectMapper().Where((s, d) => d.Name == s.Name + "Dto");
                }
            }
    
  • 相关阅读:
    yb课堂 vue里面的状态管理vuex 《四十》
    yb课堂 注册-登陆-个人模块 《三十九》
    MySQL 获取所有表名、所有表结构
    Window版 MySQL可视化工具 Navicat 面安装免激活绿色版
    yb课堂 视频详情页模块开发《三十八》
    yb课堂 首页home开发 《三十七》
    yb课堂 前端项目通用底部选项卡 CommonsFooter 《三十六》
    yb课堂 开发前端项目路由 《三十五》
    yb课堂 基于浏览器和node.js的http客户端Axios 《三十四》
    2020 团体程序设计天梯赛 个人题解 + 反思
  • 原文地址:https://www.cnblogs.com/Leman/p/5774332.html
Copyright © 2011-2022 走看看