AutoMapper2
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的更多相关文章
- 【AutoMapper官方文档】DTO与Domin Model相互转换(下)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- DTO学习系列之AutoMapper(四)
本篇目录: Mapping Inheritance-映射继承 Queryable Extensions (LINQ)-扩展查询表达式 Configuration-配置 Conditional Mapp ...
- 实现AutoMapper(1.0版本)
最近有个需求就是实体之间自动转换,网上肯定有很多现成的实现,不过还是自己写了一个,就当对java高级特性的一个熟悉的过程.这中间包含了泛型,反射,lamada表达式.对于想了解java高级特性的人来说 ...
- 手动搭建ABP2.1.3 Zero——基础框架
一.基础层搭建 二.PM.Core 三.PM.EntityFramework 四.PM.Application 五.PM.WebApi 六.PM.Web(MPA) 七.PM.Web(SPA) 八.单元 ...
- 手动搭建ABP2.1.3——基础框架
一.基础层搭建 1,创建一个空解决方案 2,层结构 Demo.Core[v:4.6.1]:类库 Demo.EntityFramework[v:4.6.1]:类库(引用Demo.Core) Demo.A ...
- automapper实体中的映射和聚合根中的使用
一,如下例子: using AutoMapper; using System; using System.Collections.Generic; using System.Linq; using S ...
随机推荐
- F# 可以把几个函数组合成新函数
C#能做的,F#基本都能做,但F#能做的,C#未必能做. F#中的函数可以把几个函数组合起来使用.下面的例子是把由 function1 和 function2 这两个函数通过运算符“>>” ...
- HTML5 Canvas前台压缩图片并上传到服务器
1.前台代码: <input id="fileOne" type="file" /> <input id="btnOne" ...
- javascript 倒计时跳转.
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 使用DataSet数据集插入记录
使用INSERT语句能够完成数据插入,使用DataSet对象也可以完成数据插入.为了将数据库的数据填充到DataSet中,则必须先使用DataAdapter对象的方法实现填充,当数据填充完成后,开发人 ...
- 用jquery向网页添加背景图片 拉伸 模糊 遮罩层 代码
方法一:手动添加 1.在body内任意位置添加html代码 <div id="web_bg" style=" position:fixed; _position:a ...
- HTML&CSS基础学习笔记1.14—有序列表及列表嵌套
我们上篇讲到了无序列表,那么今天就来看看有序列表和他们的组合嵌套使用吧. 有序列表 现在我们要做那堆杂事了,但是发现这么多杂事,先做哪个好呢?于是我们给这堆杂事弄个优先级排序,让我们能够按照顺序做下去 ...
- ASIHTTPRequest中的DELETE、PUT、GET、POST请求实例-备用
感谢分享 // ASIFormDataRequestTests.m // Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRe ...
- 怎样在Word中插入代码并保持代码原始样式不变
怎样在Word中插入代码并保持样式不变 我们有时候需要在word中添加一段我们写的代码,但是把代码粘贴到word文档中之后就发现所有的代码的样子都变了,我们可以采用下边的方法来实现保持代码原来的样式和 ...
- My advice to young people - Donald Knuth [video]
http://www.youtube.com/watch?v=75Ju0eM5T2c I took a note of what knuth said in the video. 1. Don't d ...
- JVM JMM