1.嵌套映射

 namespace Second
{
class Program
{
static void Main(string[] args)
{
Mapper.CreateMap<OuterSource, OuterDest>();
Mapper.CreateMap<InnerSource, InnerDest>();
Mapper.AssertConfigurationIsValid();
var source = new OuterSource
{
Value = ,
Inner = new InnerSource { OtherValue= }
};
var dest = Mapper.Map<OuterSource, OuterDest>(source);
}
} public class OuterDest
{
public int Value { get; set; }
public InnerDest Inner { get; set; }
} public class InnerDest
{
public int OtherValue { get; set; }
} public class OuterSource
{
public int Value { get; set; }
public InnerSource Inner { get; set; }
}
public class InnerSource
{
public int OtherValue { get; set; }
}
}

2.自定义类型转换

 class Program
{
static void Main(string[] args)
{
Mapper.CreateMap<string, int>().ConvertUsing(Convert.ToInt32);
Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
// Mapper.CreateMap<string, DateTime>().ConvertUsing(Convert.ToDateTime);
Mapper.CreateMap<string, Type>().ConvertUsing<TypeTypeConverter>();
Mapper.CreateMap<Source, Destination>();
Mapper.AssertConfigurationIsValid();
var source = new Source
{
Value1 = "",
Value2 = "01/01/2000",
Value3 = "AutoMapperSamples.GlobalTypeConverters.GlobalTypeConverters+Destination"
};
var result= Mapper.Map<Source, Destination>(source); }
}
public class DateTimeTypeConverter : ITypeConverter<string,DateTime>
{
public DateTime Convert(ResolutionContext context)
{
return System.Convert.ToDateTime(context.SourceValue);
}
}
public class TypeTypeConverter : ITypeConverter<string, Type>
{
public Type Convert(ResolutionContext context)
{
return context.SourceType;
}
} public class Source
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 { get; set; }
}
public class Destination
{
public int Value1 { get; set; }
public DateTime Value2 { get; set; }
public Type Value3 { get; set; }
}

3.自定义值转换

  class Program
{
static void Main(string[] args)
{
//Mapper.CreateMap<Source, Destination>()
// .ForMember(a => a.Total, b => b.ResolveUsing<CustomResolver>()); //不使用反射构造CustomResolver
Mapper.CreateMap<Source, Destination>()
.ForMember(opt => opt.Total,
src => src.ResolveUsing<CustomResolver>()
.ConstructedBy(()=>new CustomResolver())); var source = new Source
{
Value1 = ,
Value2 =
}; var result = Mapper.Map<Source, Destination>(source); }
} public class CustomResolver : ValueResolver<Source, int>
{
protected override int ResolveCore(Source source)
{
return source.Value1 + source.Value2;
}
} public class Source
{
public int Value1 { get; set; }
public int Value2 { get; set; }
} public class Destination
{
public int Total { get; set; }
}

4.替代NULL

 class Program
{
static void Main(string[] args)
{
Mapper.CreateMap<Source, Dest>()
.ForMember(dest => dest.Value, opt => opt.NullSubstitute("hi"));
var source = new Source { Value=null};
var d = Mapper.Map<Source, Dest>(source); Console.WriteLine(d.Value);//hi
}
} class Source
{
public string Value { get; set; }
} class Dest
{
public string Value { get; set; }
}

5.在map之前或之后进行处理

 class Program
{
static void Main(string[] args)
{
//Mapper.CreateMap<Source, Dest>()
// .BeforeMap((src, dest) => src.Value = src.Value + "hh")
// .AfterMap((src,dest)=>dest.Value="glzsk"); Mapper.CreateMap<Source, Dest>(); var a = new Source { Value="A" };
// var b = Mapper.Map<Dest>(a);
var b = Mapper.Map<Source, Dest>(a, opt =>
{
opt.AfterMap((src, dest) => dest.Value = "glzsk");
opt.BeforeMap((src, dest) => src.Value = src.Value + "hh");
}
);
Console.WriteLine(a.Value+"\r\n"+b.Value);
Console.ReadLine();
}
} class Source
{
public string Value { get; set; }
} class Dest
{
public string Value { get; set; }
}

AutoMapper2的更多相关文章

  1. 【AutoMapper官方文档】DTO与Domin Model相互转换(下)

    写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...

  2. DTO学习系列之AutoMapper(四)

    本篇目录: Mapping Inheritance-映射继承 Queryable Extensions (LINQ)-扩展查询表达式 Configuration-配置 Conditional Mapp ...

  3. 实现AutoMapper(1.0版本)

    最近有个需求就是实体之间自动转换,网上肯定有很多现成的实现,不过还是自己写了一个,就当对java高级特性的一个熟悉的过程.这中间包含了泛型,反射,lamada表达式.对于想了解java高级特性的人来说 ...

  4. 手动搭建ABP2.1.3 Zero——基础框架

    一.基础层搭建 二.PM.Core 三.PM.EntityFramework 四.PM.Application 五.PM.WebApi 六.PM.Web(MPA) 七.PM.Web(SPA) 八.单元 ...

  5. 手动搭建ABP2.1.3——基础框架

    一.基础层搭建 1,创建一个空解决方案 2,层结构 Demo.Core[v:4.6.1]:类库 Demo.EntityFramework[v:4.6.1]:类库(引用Demo.Core) Demo.A ...

  6. automapper实体中的映射和聚合根中的使用

    一,如下例子: using AutoMapper; using System; using System.Collections.Generic; using System.Linq; using S ...

随机推荐

  1. (转)jQuery Validation Plugin客户端表单证验插件

    jQuery Validation Plugin客户端表单验证插件 官方文档:http://jqueryvalidation.org/documentation/ 官方demo:http://jque ...

  2. Search 和 Select比较 - 浅谈

    Search 语法: public IFeatureCursor Search (    IQueryFilter filter,    bool Recycling); Select 语法: pub ...

  3. C#string与char互转

    string s = "asdf"; //字符转char char[] c = s.ToCharArray(); Console.WriteLine(s[]); //char转st ...

  4. const的重载

    class A { private: int a; public: A(int x) :a(x){}//构造函数并不能重载 void display(){ cout << "no ...

  5. 实例讲解MySQL联合查询

    好了终于贴完了MySQL联合查询的内容了,加上上一篇一共2篇,都是我转载的,实例讲解MySQL联合查询.那下面就具体讲讲简单的JOIN的用法了.首先我们假设有2个表A和B,他们的表结构和字段分别为: ...

  6. HTML标签区别

    一.div和span的区别 div是一个块级元素,可以包含段落,表格等内容,用于放置不同的内容.一般我们在网页通过div来布局定位网页中的每个区块. span是一个内联元素,没有实际意义,它的存在纯粹 ...

  7. Symfony2中的设计模式——装饰者模式

    装饰者模式的定义  文章链接:http://www.hcoding.com/?p=101 个人站点:http://www.hcoding.com/ 在不必改变原类文件和使用继承的情况下,动态地扩展一个 ...

  8. HDFS操作--文件上传/创建/删除/查询文件信息

    1.上传本地文件到HDFS //上传本地文件到HDFS public class CopyFile { public static void main(String[] args) { try { C ...

  9. 遗传算法matlab实现

    我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下运用MATLAB实现遗传算法:   clc clear   %参数 a = 0 ; b = 4 ; e ...

  10. HSV颜色识别demo

    HSV(Hue, Saturation, Value)色彩空间是一种区别与RGB的表示形式.其模型可视为一个倒立的棱锥或圆锥. 其中H为色调,用角度度量,取值范围为0°-360°,从红色开始按逆时针方 ...