|
第一步:了解类库方法:TinyMapper 主要有两个函数:
- TinyMapper.Bind<T1, T2>();//绑定映射关系
- TinyMapper.Map<T>(obj);//从对象获取想要的对象
复制代码
第二步:初始化Mapping设置
说明:类似的Mapping设置无非两种模式:代码静态对象初始化模式,xml配置模式,很荣幸TinyMapper支持的是静态字典。
- public static class TinyMapContext
- {
- public static void InitMapping()
- {
- TinyMapper.Bind<Person, PersonDto>();
- TinyMapper.Bind<Person, PersonDto>(config =>
- {
- config.Ignore(x => x.Id);//忽略ID字段
- config.Bind(x => x.Name, y => y.UserName);//将源类型和目标类型的字段对应绑定起来
- config.Bind(x => x.Age, y => y.Age);//将源类型和目标类型的字段对应绑定起来
- });
- TinyMapper.Bind<Person, PersonDto>(config =>
- {
- config.Ignore(x => x.Id);//忽略ID字段
- //将源类型和目标类型的字段对应绑定起来
- config.Bind(x => x.Name, y => y.UserName);
- config.Bind(x => x.Age, y => y.Age);
- config.Bind(x => x.Address, y => y.Address);
- config.Bind(x => x.Emails, y => y.Emails);
- });
- TinyMapper.Bind<PersonDto,Person>();
- TinyMapper.Bind<PersonDto,Person>(config =>
- {
- config.Bind(x => x.Id,y=>y.Id);
- config.Bind(x => x.UserName, y => y.Name);
- config.Bind(x => x.Age, y => y.Age);
- });
- TinyMapper.Bind<PersonDto,Person>(config =>
- {
- config.Bind(x => x.Id,y=>y.Id);//忽略ID字段
- //将源类型和目标类型的字段对应绑定起来
- config.Bind(x =>x.UserName,y=> y.Name);
- config.Bind(x => x.Age, y => y.Age);
- config.Bind(x => x.Address, y => y.Address);
- config.Bind(x => x.Emails, y => y.Emails);
- });
- }
- public static T GetMapObject<T>(object obj) where T:class
- {
- return TinyMapper.Map<T>(obj);
- }
- }
复制代码 说明:以上mapping映射中,针对于原作者的代码,额外添加了:由PersonDto=》Person的映射关系。
T GetMapObject<T>(object obj) where T:class 的作用会在后面的代码中体现出来。 简单一个方法,威力不可小嘘~~
第三步:DtoModel -》Model
从数据库模型映射到领域模型:
- var p = TinyMapContext.GetMapObject<Person>(personDto);
复制代码
第四步:Model-》DtoModel
从领域模型到数据库模型:
- var personDto = TinyMapContext.GetMapObject<PersonDto>(person);
复制代码
第五步:List<Model>=>List<DtoModel> 或者List<DtoModel>=>List<Model>
- /// <summary>
- /// 测试列表对象。
- /// </summary>
- static void Test4()
- {
- List<Person> personList = new List<Person>(){
- new Person
- {
- Id = Guid.NewGuid().ToString(),
- Name = "John1",
- Age = 22,
- Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" },
- Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" }
- },
- new Person
- {
- Id = Guid.NewGuid().ToString(),
- Name = "John2",
- Age = 22,
- Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" },
- Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" }
- },
- new Person
- {
- Id = Guid.NewGuid().ToString(),
- Name = "John3",
- Age = 22,
- Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" },
- Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" }
- },
- new Person
- {
- Id = Guid.NewGuid().ToString(),
- Name = "John4",
- Age = 22,
- Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" },
- Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" }
- }
- };
- var personDtoList = TinyMapContext.GetMapObject<List<PersonDto>>(personList);
- foreach (var item in personDtoList)
- {
- DebugOutputUtil.DebugOutput(item.UserName);
- }
- var pList = TinyMapContext.GetMapObject<List<Person>>(personDtoList);
- foreach (var item in pList)
- {
- DebugOutputUtil.DebugOutput(item.Name);
- }
- }
复制代码
特别说明:对于获取列表类型的对象,不需要在静态初始化映射中额外添加 IList类型的对象。 TinyMapper会自动按照"映射过的基础类型"动态的帮你获取想要的数据。
参考链接:
.NET平台开源项目速览(14)最快的对象映射组件Tiny Mapper http://www.cnblogs.com/asxinyu/p ... ect_TinyMapper.html
EmitMapper 和TinyMapper 两者简单对比 https://blog.csdn.net/umke888/article/details/54880670
|
上一篇:Newtonsoft.Json忽略DataContract特性下一篇:c#线程学习之ManualResetEvent和AutoResetEvent的区别
|