标签:
TinyMapper - a tiny and quick object mapper for .Net.
The main advantage is performance. TinyMapper allows easily map object to object, i.e. properties or fields from one object to another, for instance.
To install TinyMapper, run the following command in the Package Manager Console
Install-Package TinyMapper
Code Example1
Person.cs
public class Person { public string Address { get; set; } public string Email { get; set; } public string FirstName { get; set; } public Guid Id { get; set; } public string LastName { get; set; } }
PersonDto.cs
public class PersonDto { public string Email { get; set; } public string FirstName { get; set; } public Guid Id { get; set; } public string LastName { get; set; } }
Main
static void Main(string[] args) { //First of all Bind Person type to PersonDto type and we don‘t want map Email property. So it has been ignored. TinyMapper.Bind<Person, PersonDto>(config => { config.Ignore(x => x.Email); }); var person = new Person { Id = Guid.NewGuid(), FirstName = "Luke", LastName = "Chen", Email = "xiaoyong6906@126.com" }; //Now TinyMapper knows how to map Person object to PersonDto object. Finally, call PersonDto personDto = TinyMapper.Map<PersonDto>(person); Console.WriteLine("personDto:" + personDto.Id + personDto.FirstName + personDto.LastName + personDto.Email); Console.ReadLine(); }
Code Example2
PersonComplex.cs
public class PersonComplex { public Address Address { get; set; } public DateTime CreateTime { get; set; } public List<string> Emails { get; set; } public string FirstName { get; set; } public Guid Id { get; set; } public string LastName { get; set; } public string Nickname { get; set; } }
PersonDtoComplex.cs
public class PersonDtoComplex { public Address Address { get; set; } public DateTime CreateTime { get; set; } public List<string> Emails { get; set; } public string FirstName { get; set; } public Guid Id { get; set; } public string LastName { get; set; } public string Nickname { get; set; } }
Main
static void Main(string[] args) { TinyMapper.Bind<PersonComplex, PersonDtoComplex>(config => { config.Ignore(x => x.CreateTime); config.Ignore(x => x.Nickname); config.Bind(x => x.FirstName, y => y.FirstName);//order property name }); var person = new PersonComplex { Id = Guid.NewGuid(), FirstName = "Luke", LastName = "Chen", Address = new Address { Phone = "XXXX", Street = "IT Street", ZipCode = "424600" }, CreateTime = DateTime.Now, Nickname = "Yong", Emails = new List<string> { "xiaoyong6906@126.com", "xiaoyong6907@126.com" } }; var personDto = TinyMapper.Map<PersonDtoComplex>(person); Console.WriteLine(personDto.Nickname == null); Console.WriteLine(personDto.FirstName); Console.ReadLine(); }
Code Example3
TargetClass.cs
public class TargetClass { public string FullName { get; set; } }
SourceClass.cs
public class SourceClass { public string FirstName { get; set; } public string LastName { get; set; } }
SourceClassConverter.cs
public class SourceClassonverter : TypeConverter { public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return destinationType == typeof(TargetClass); } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { var concreteValue = (SourceClass)value; var result = new TargetClass { FullName = string.Format("{0} {1}", concreteValue.FirstName, concreteValue.LastName) }; return result; } }
Main
static void Main(string[] args) { TinyMapper.Bind<SourceClass, TargetClass>(); var source = new SourceClass { FirstName = "Luke", LastName = "Chen" }; var result = TinyMapper.Map<TargetClass>(source); Console.WriteLine(result.FullName); Console.ReadLine(); }
标签:
原文地址:http://www.cnblogs.com/chenyongblog/p/5492366.html