先引用对应的DLL.

11.转换匿名对象 结合LINQ映射新的实体类.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //1.普通转换
            Name name1 = new Name() { FirstName = "L", LastName = "jz" };
            Mapper.CreateMap<Name, NameDto>()
                .BeforeMap((name, nameDto) => Console.WriteLine("hello world before"))
                .AfterMap((name, nameDto) => Console.WriteLine("hello world after"));
            NameDto nameDto1 = Mapper.Map<Name, NameDto>(name1);
            Console.WriteLine("1");
            Console.WriteLine(nameDto1.FirstName + nameDto1.LastName);
            Console.WriteLine();
            //Console.ReadKey();

//整体设置
            //2.整体即时转换
            Mapper.Reset();
            Name name2 = new Name() { FirstName = "L", LastName = "jz" };
            Mapper.CreateMap<Name, NameDto>()
                .ConstructUsing(name => new NameDto() { AllName = name.FirstName + name.LastName });
            NameDto nameDto2 = Mapper.Map<Name, NameDto>(name2);
            Console.WriteLine("2");
            Console.WriteLine(nameDto2.AllName);
            Console.WriteLine();
            //Console.ReadKey();

//3.整体通过TypeConverter类型转换
            Mapper.Reset();
            Name name3 = new Name() { FirstName = "L", LastName = "jz" };
            Mapper.CreateMap<Name, NameDto>()
                .ConvertUsing<NameConverter>();
            NameDto nameDto3 = Mapper.Map<Name, NameDto>(name3);
            Console.WriteLine("3");
            Console.WriteLine(nameDto3.AllName);
            Console.WriteLine();
            //Console.ReadKey();

//单属性设置
            //4.属性条件转换
            Mapper.Reset();
            Name name4 = new Name() { FirstName = "L", LastName = "jz" };
            Mapper.CreateMap<Name, NameDto>()
                .ForMember(name => name.FirstName, opt => opt.Condition(name => !name.FirstName.Equals("l", StringComparison.OrdinalIgnoreCase)));
            NameDto nameDto4 = Mapper.Map<Name, NameDto>(name4);
            Console.WriteLine("4");
            Console.WriteLine(string.IsNullOrEmpty(nameDto4.FirstName));
            Console.WriteLine();
            //Console.ReadKey();

//5.属性忽略
            Mapper.Reset();
            Name name5 = new Name() { FirstName = "L", LastName = "jz" };
            Mapper.CreateMap<Name, NameDto>()
                .ForMember(name => name.FirstName, opt => opt.Ignore());
            NameDto nameDto5 = Mapper.Map<Name, NameDto>(name5);
            Console.WriteLine("5");
            Console.WriteLine(string.IsNullOrEmpty(nameDto5.FirstName));
            Console.WriteLine();
            //Console.ReadKey();

//6.属性转换
            Mapper.Reset();
            Name name6 = new Name() { FirstName = "L", LastName = "jz" };
            Mapper.CreateMap<Name, NameDto>()
                .ForMember(name => name.AllName, opt => opt.MapFrom(name => name.FirstName + name.LastName));
            NameDto nameDto6 = Mapper.Map<Name, NameDto>(name6);
            Console.WriteLine("6");
            Console.WriteLine(nameDto6.AllName);
            Console.WriteLine();
            //Console.ReadKey();

//7.属性通过ValueResolver转换
            Mapper.Reset();
            Name name7 = new Name() { FirstName = "L", LastName = "jz" };
            Mapper.CreateMap<Name, StoreDto>()
                .ForMember(storeDto => storeDto.Name, opt => opt.ResolveUsing<NameResolver>());
            StoreDto store1 = Mapper.Map<Name, StoreDto>(name7);
            Console.WriteLine("7");
            Console.WriteLine(store1.Name.FirstName + store1.Name.LastName);
            Console.WriteLine();
            //Console.ReadKey();

//8.属性填充固定值
            Mapper.Reset();
            Name name8 = new Name() { FirstName = "L", LastName = "jz" };
            Mapper.CreateMap<Name, NameDto>()
                .ForMember(name => name.AllName, opt => opt.UseValue<string>("ljzforever"));
            NameDto nameDto8 = Mapper.Map<Name, NameDto>(name8);
            Console.WriteLine("8");
            Console.WriteLine(nameDto8.AllName);
            Console.WriteLine();
            //Console.ReadKey();

//9.属性格式化
            Mapper.Reset();
            Name name9 = new Name() { FirstName = "L", LastName = "jz" };
            Mapper.CreateMap<Name, NameDto>()
                .ForMember(name => name.FirstName, opt => opt.AddFormatter<StringFormatter>());
            NameDto nameDto9 = Mapper.Map<Name, NameDto>(name9);
            Console.WriteLine("9");
            Console.WriteLine(nameDto9.FirstName);
            Console.WriteLine();
            //Console.ReadKey();

//10.属性null时的默认值
            Mapper.Reset();
            Name name10 = new Name() { FirstName = "L" };
            Mapper.CreateMap<Name, NameDto>()
                .ForMember(name => name.LastName, opt => opt.NullSubstitute("jz"));
            NameDto nameDto10 = Mapper.Map<Name, NameDto>(name10);
            Console.WriteLine("10");
            Console.WriteLine(nameDto10.LastName);
            Console.WriteLine();
            //Console.ReadKey();

//其它设置与特性
            //11.转换匿名对象
            Mapper.Reset();
            object name11 = new { FirstName = "L", LastName = "jz" };
            NameDto nameDto11 = Mapper.DynamicMap<NameDto>(name11);
            Console.WriteLine("11");
            Console.WriteLine(nameDto11.FirstName + nameDto11.LastName);
            Console.WriteLine();
            //Console.ReadKey();

//12.转换DataTable
            Mapper.Reset();
            DataTable dt = new DataTable();
            dt.Columns.Add("FirstName", typeof(string));
            dt.Columns.Add("LastName", typeof(string));
            dt.Rows.Add("L", "jz");
            List<NameDto> nameDto12 = Mapper.DynamicMap<IDataReader, List<NameDto>>(dt.CreateDataReader());
            Console.WriteLine("12");
            Console.WriteLine(nameDto12[0].FirstName + nameDto12[0].LastName);
            Console.WriteLine();
            //Console.ReadKey();
            //emitMapper error
            //List<NameDto> nameDto20 = EmitMapper.ObjectMapperManager.DefaultInstance.GetMapper<IDataReader, List<NameDto>>().Map(dt.CreateDataReader());

//13.转化存在的对象
            Mapper.Reset();
            Mapper.CreateMap<Name, NameDto>()
                .ForMember(name => name.LastName, opt => opt.Ignore());
            Name name13 = new Name() { FirstName = "L" };
            NameDto nameDto13 = new NameDto() { LastName = "jz" };
            Mapper.Map<Name, NameDto>(name13, nameDto13);
            //nameDto13 = Mapper.Map<Name, NameDto>(name13);//注意,必需使用上面的写法,不然nameDto13对象的LastName属性会被覆盖
            Console.WriteLine("13");
            Console.WriteLine(nameDto13.FirstName + nameDto13.LastName);
            Console.WriteLine();
            //Console.ReadKey();

//14.Flatten特性
            Mapper.Reset();
            Mapper.CreateMap<Store, FlattenName>();
            Store store2 = new Store() { Name = new Name() { FirstName = "L", LastName = "jz" } };
            FlattenName nameDto14 = Mapper.Map<Store, FlattenName>(store2);
            Console.WriteLine("14");
            Console.WriteLine(nameDto14.NameFirstname + nameDto14.NameLastName);
            Console.WriteLine();
            //Console.ReadKey();

//15.将Dictionary转化为对象,现在还不支持
            Mapper.Reset();
            Mapper.CreateMap<Dictionary<string, object>, Name>();
            Dictionary<string, object> dict = new Dictionary<string, object>();
            dict.Add("FirstName", "L");
            //Name name15 = Mapper.DynamicMap<Dictionary<string, object>, Name>(dict);
            Name name15 = Mapper.Map<Dictionary<string, object>, Name>(dict);
            Console.WriteLine("15");
            Console.WriteLine(name15.FirstName);
            Console.WriteLine();
            Console.ReadKey();
        }
    }

public class Store
    {
        public Name Name { get; set; }
        public int Age { get; set; }
    }

public class Name
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

public class StoreDto
    {
        public NameDto Name { get; set; }
        public int Age { get; set; }
    }

public class NameDto
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string AllName { get; set; }
    }

public class FlattenName
    {
        public string NameFirstname { get; set; }
        public string NameLastName { get; set; }
    }

public class NameConverter : TypeConverter<Name, NameDto>
    {
        protected override NameDto ConvertCore(Name source)
        {
            return new NameDto() { AllName = source.FirstName + source.LastName };
        }
    }

public class NameResolver : ValueResolver<Name, NameDto>
    {
        protected override NameDto ResolveCore(Name source)
        {
            return new NameDto() { FirstName = source.FirstName, LastName = source.LastName };
        }
    }

public class NameFormatter : ValueFormatter<NameDto>
    {
        protected override string FormatValueCore(NameDto name)
        {
            return name.FirstName + name.LastName;
        }
    }

public class StringFormatter : ValueFormatter<string>
    {
        protected override string FormatValueCore(string name)
        {
            return name + "-";
        }
    }
}

AutoMapper 自动映射工具的更多相关文章

  1. AutoMapper自动映射

    十年河东,十年河西,莫欺少年穷. 学无止境,精益求精. 不扯犊子,直接进入正题: AutoMapper自动映射常用于EF中,能很好的解决DTO和Model之间相互映射的问题.在未使用AutoMappe ...

  2. .NET之AutoMapper对象映射工具运用

    AutoMapper对象映射工具:主要是将某一个实体转成另一个实体. 1.引用NuGet包;搜索:AutoMapper 2.创建实体类 using System; using System.Colle ...

  3. EmitMapper自动映射工具

             在实体与DTO之间,我们一般都需要进行映射.如果手动的来进行转换,实在是太麻烦.所以就产生了很多映射工具,比如AutoMapper,EmitMapper.而经过一些对比,EmitMa ...

  4. EF架构~AutoMapper对象映射工具简化了实体赋值的过程

    回到目录 AutoMapper是一个.NET的对象映射工具,一般地,我们进行面向服务的开发时,都会涉及到DTO的概念,即数据传输对象,而为了减少系统的负载,一般我们不会把整个表的字段作为传输的数据,而 ...

  5. automapper 自动映射 集成asp.net Core2.1

    学习博文:https://www.cnblogs.com/yan7/p/8085410.html 1.使用nuget 安装 <PackageReference Include="Aut ...

  6. Asp.NetCore 3.1 使用AutoMapper自动映射转换实体 DTO,Data2ViewModel

    1:什么是AutoMapper? 下面为AutoMapper官方的解释: AutoMapper是一个对象-对象映射器.对象-对象映射通过将一种类型的输入对象转换为另一种类型的输出对象来工作. 使Aut ...

  7. 使用SpringDataJdbc的@Query注解实现自动映射结果集 ----- RowMapper接口

    使用@Query注解的时候,常常需要写sql来映射非域类的实例,通常的做法就是 实现 RowMapper接口,然后new实例一个一个的设置值进去...为此.出世了自动映射工具类 注意事项:此抽象类只是 ...

  8. .NET的DTO映射工具AutoMapper

    .NET的DTO映射工具AutoMapper 原文:https://github.com/AutoMapper/AutoMapper/wiki/Getting-started 参考:http://ww ...

  9. .NET的对象映射工具AutoMapper使用笔记

    AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/Aut ...

随机推荐

  1. C语言函数参数的传递详解

    一.三道考题 开讲之前,我先请你做三道题目.(嘿嘿,得先把你的头脑搞昏才行--唉呀,谁扔我鸡蛋?)考题一,程序代码如下:void Exchg1(int x, int y){   int tmp;    ...

  2. 解决Fileupload 在IE 10 中需要双击的问题

    方法来自于:https://code.google.com/p/primefaces/issues/detail?id=5355 the oncomplete event is working for ...

  3. STS中Maven配置

    最近接触maven, 配置过程中记录一下. STS是解压版的,启动后,可以看到已经有了Maven插件, , 但是,STS也同时给你了一个Maven,但是通常不建议使用STS自带的maven.使用默认的 ...

  4. Java中自己实现枚举

    public class MyEnum { private final String name; public  static final MyEnum red = new MyEnum(" ...

  5. POJ 2977 Box walking

    题目链接:http://poj.org/problem?id=2977 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 222 ...

  6. hdu_5821_Ball(贪心)

    题目链接:hdu_5821_Ball 题意: 给你序列A和序列B,依次给你m个区间,每次你可以交换区间内的任意数,问最后能不能将A变成B 题解: 比赛当时没想到,智商被压制 1001: 假设有4个红球 ...

  7. Leetcode016 3Sum Closest

    public class S016 { //借鉴S015的思想,只是稍微有点慢 public int threeSumClosest(int[] nums, int target) { Arrays. ...

  8. 让 idea webstorm phpstorm 能够 识别 thinkphp 的方法(自动提示功能)

    1.在/ThinkPHP/Library/Think 目录下 新建一个文件,名为:  BaseController.class.php 2.BaseController.class.php 内容为 n ...

  9. NOIP2014-普及组复赛-第二题-比例简化

    题目描述 Description 在社交媒体上,经常会看到针对某一个观点同意与否的民意调查以及结果.例如,对某一观点表示支持的有1498 人,反对的有 902人,那么赞同与反对的比例可以简单的记为14 ...

  10. c# 操作word demo

    /// <summary> /// 新创建word /// </summary> /// <param name="fileSaveDirectory" ...