码迷,mamicode.com
首页 > 移动开发 > 详细

记-TinyMapper使用

时间:2016-05-14 14:05:55      阅读:481      评论:0      收藏:0      [点我收藏+]

标签:

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.

技术分享

  • How to install TinyMapper

To install TinyMapper, run the following command in the Package Manager Console

Install-Package TinyMapper

  • How To Use 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; }
    }
View Code

       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; }
    }
View Code

      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();
        }
View Code

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; }
    }
View Code

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; }
    }
View Code

 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();
        }
View Code

Code Example3

TargetClass.cs

技术分享
    public class TargetClass
    {
        public string FullName { get; set; }
    }
View Code

SourceClass.cs

技术分享
    public class SourceClass
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
View Code

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;
        }
    }
View Code

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();
        }
View Code

 

 

 

记-TinyMapper使用

标签:

原文地址:http://www.cnblogs.com/chenyongblog/p/5492366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!