zoukankan      html  css  js  c++  java
  • AutoMapper Profile用法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using AutoMapper;
    
    namespace MvcAutoMapper.AutoMapper
    {
        public class Configuration
        {
            public static void Configure()
            {
                Mapper.Initialize(cfg =>
                {
                    cfg.AddProfile<UserProfile>();
                });
            }
        }
    }
    

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using AutoMapper;
    
    namespace MvcAutoMapper.AutoMapper.Profiles
    {
        public class UserProfile:Profile
        {
    
            protected override void Configure()
            {
                CreateMap<Models.User, Models.UserDto>();
    
            }
        }
    }
    

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using AutoMapper;
    
    namespace MyMvc.AutoMapper
    {
        public static class AutoMapperForMvc
        {
            public static void Register()
            {
                Mapper.Initialize(x =>
                {
                    x.AddProfile<UserProfile>();
                });
    
                //在程序启动时对所有的配置进行严格验证
                Mapper.AssertConfigurationIsValid();
            }
        }
    }
    

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using AutoMapper;
    
    namespace MyMvc.AutoMapper
    {
        public class UserProfile:Profile
        {
            public UserProfile()
            {
                base.CreateMap<Models.User, Models.UserView>()
                    //映射前
                    .BeforeMap((s, t) =>
                    {
                        s.Age += 10;
                    })
                    //映射后
                    .AfterMap((s, t) =>
                    {
                        t.Age += 10;
                    })
                    //条件判断 
                    //年龄不大于10 不映射年龄这个属性的值,那值就是默认的0,但是映射后+10,所以就是10
                    .ForMember(t=>t.Age,o=>o.Condition(s=>s.Age>30))
                    //空值
                    .ForMember(t=>t.Name, o=>o.NullSubstitute("无名氏")) 
                    .ForMember(x=>x.time,x=>x.MapFrom(s=>s.add_time))
                    //反向映射
                    .ReverseMap(); 
            }
        }
    }
    

      

  • 相关阅读:
    Android TextView中实现点击文本超链接(无下划线)的封装类
    first day for new job
    配置Log4j(非常具体)
    验证数字的正则表达式集
    虚拟化知识点
    Java实现第十届蓝桥杯数的分解
    Java实现第十届蓝桥杯数的分解
    Java实现第十届蓝桥杯数的分解
    Java实现第十届蓝桥杯数列求值
    Java实现第十届蓝桥杯数列求值
  • 原文地址:https://www.cnblogs.com/Jeely/p/11004292.html
Copyright © 2011-2022 走看看