• [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

     

     

  • 相关阅读:
    Jquery事件
    基础:装箱和拆箱...
    navicat编辑表的作用
    谷歌浏览器preview展示问题
    @Scheduled并行执行
    spring异步执行方法线程池的配置
    dubbo的ExceptionFilter异常处理
    dubbo异常处理
    idea设置启动jvm参数
    前后端分离走本地代码Charles的使用
  • 原文地址:https://www.cnblogs.com/chenyongblog/p/5492366.html
Copyright © 2020-2023  润新知