zoukankan      html  css  js  c++  java
  • [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

     

     

  • 相关阅读:
    java工作复习——执行JS脚本——滚动条01
    java工作复习——cookie的增删查
    java工作复习——4大时间等待——显示等待(转载)
    java工作复习——4大时间等待——显示等待
    java工作复习——4大时间等待——隐式等待等待(driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);)
    java工作复习——4大时间等待——强制等待(Thread.sleep(5000);)
    java工作复习——键盘事件——action类——复制、粘贴 (转载)
    java工作复习——键盘事件——action类——复制、粘贴
    java工作复习——鼠标事件——action类——转载总结
    java工作复习——鼠标事件——action类——模拟鼠标从一个位置移动到另外一个位置
  • 原文地址:https://www.cnblogs.com/chenyongblog/p/5492366.html
Copyright © 2011-2022 走看看