1.Tiny Mapper的简单实用例子
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Nelibur.ObjectMapper; using Nelibur.ObjectMapper.Bindings; namespace Test { class Program { static void Main(string[] args) { TinyMapper.Bind<Person, PersonDto>(); //实例化一个Person对象 var person = new Person { Id = Guid.NewGuid().ToString(), Name = "John", Age = 22 }; //映射 var personDto = TinyMapper.Map<PersonDto>(person); Console.WriteLine("Id:{0},Name:{1},Age:{2}", personDto.Id, personDto.Name, personDto.Age); Console.ReadLine(); } } public class Person { public String Id { get; set; } public String Name { get; set; } public Int32 Age { get; set; } } public class PersonDto { public String Id { get; set; } public String Name { get; set; } public Int32 Age { get; set; } } }
2.Tiny Mapper 指定配置使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Nelibur.ObjectMapper; using Nelibur.ObjectMapper.Bindings; namespace Test { class Program { static void Main(string[] args) { 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);//将源类型和目标类型的字段对应绑定起来 }); var person = new Person { Id = Guid.NewGuid().ToString(), Name = "John", Age = 22 }; var personDto = TinyMapper.Map<PersonDto>(person); Console.WriteLine("Id:{0},Name:{1},Age:{2}", personDto.Id, personDto.UserName, personDto.Age); Console.ReadLine(); } } public class Person { public String Id { get; set; } public String Name { get; set; } public Int32 Age { get; set; } } public class PersonDto { public String Id { get; set; } public String UserName { get; set; } public Int32 Age { get; set; } } }
3.Tiny Mapper复杂类型使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Nelibur.ObjectMapper; using Nelibur.ObjectMapper.Bindings; namespace Test { class Program { static void Main(string[] args) { 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); }); var person = new Person { Id = Guid.NewGuid().ToString(), Name = "John", Age = 22, Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" }, Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" } }; var personDto = TinyMapper.Map<PersonDto>(person); } } public class Person { public String Id { get; set; } public String Name { get; set; } public Int32 Age { get; set; } public Address Address { get; set; } public List<String> Emails { get; set; } } public class PersonDto { public String Id { get; set; } public String UserName { get; set; } public Int32 Age { get; set; } public Address Address { get; set; } public List<String> Emails { get; set; } } public sealed class Address { public string Phone { get; set; } public string Street { get; set; } public string ZipCode { get; set; } } }