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 ...
随机推荐
- MySQL 远程访问开启
打开mysql客户端,直接运行以下命令:1.use mysql; 2.update user set host='%' where user='root'; 会报错:ERROR 1062 (23000 ...
- UIImageView中最容易用错的属性UIContentMode小记
UIContentMode这东西初学真是各种问题,只能不断尝试,偶然发现网上这张图片,做下记录 还有个UIViewContentModeRedraw,在使用这个设置后,每当图片的bounds就是大小或 ...
- cordova-plugin-app-version插件使用
此插件用来获取开发软件的版本号! 首先安装此插件: 命令行中输入 cordova plugin add cordova-plugin-app-version 然后刷新项目,就会在在项目plugin ...
- MVC中,加入的一个aspx页面用到AspNetPager控件处理办法
今天项目遇到了如题所示的问题,按照官方的案例介绍做分页,简直要奔溃了, 使用URL重写,但是page总是1,根本不跳, 不使用URL重写,又出现,第一页是 http://aa.com/view_asp ...
- notepad++搜索结果不显示line XX的方法
在使用notepad++如果多次搜索,得到的结果中会出现多次line xx: line xx:,造成文件大量垃圾信息的存在,不利于找寻所需的内容,如下图. 对于这种情况, ...
- uC/OS-II内核架构解析(1)---嵌入式RTOS(转)
uC/OS-II内核架构解析(1)---嵌入式RTOS 1. 嵌入式系统基本模型 2. RTOS设计原则 采用各种算法和策略,始终保持系统行为的可预测性.即在任何情况下,在系统运行的任何时刻,OS的资 ...
- ios晃动检测
ios晃动检测 第一种 1.在AppDelegate.h中进行如下设置: - (BOOL)application:(UIApplication *)application didFinishLaun ...
- cf B. Fence
http://codeforces.com/contest/363/problem/B #include <cstdio> #include <cstring> #includ ...
- Unity脚本的生命周期中几个重要的方法
1.function Update () {} 正常更新,用于更新逻辑.此方法每帧都会由系统自动调用一次.2.function LateUpdate () {} 推迟更新,此方法在Update() 方 ...
- WPF学习拾遗(二)TextBlock换行
原文:WPF学习拾遗(二)TextBlock换行 下午在帮组里的同事解决一个小问题,为了以后方便,把就把它收集一下吧. 新建一个TextBlock作为最基础的一个控件,他所携带的功能相对于其他的控件要 ...