[C#] 记-TinyMapper使用

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

 

 

 

转载于:https://www.cnblogs.com/chenyongblog/p/5492366.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值