对象映射组件Tiny Mapper

1.Tiny Mapper的简单实用例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nelibur.ObjectMapper;
using Nelibur.ObjectMapper.Bindings;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            TinyMapper.Bind<Person, PersonDto>();
            //实例化一个Person对象
            var person = new Person
            {
                Id = Guid.NewGuid().ToString(),
                Name = "John",
                Age = 22
            };
            //映射
            var personDto = TinyMapper.Map<PersonDto>(person);
            Console.WriteLine("Id:{0},Name:{1},Age:{2}", personDto.Id, personDto.Name, personDto.Age);
            Console.ReadLine();
        }
    }

    public class Person
    {
        public String Id { get; set; }
        public String Name { get; set; }
        public Int32 Age { get; set; }
    }
    public class PersonDto
    {
        public String Id { get; set; }
        public String Name { get; set; }
        public Int32 Age { get; set; }
    }
}

2.Tiny Mapper 指定配置使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nelibur.ObjectMapper;
using Nelibur.ObjectMapper.Bindings;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            TinyMapper.Bind<Person, PersonDto>(config =>
            {
                config.Ignore(x => x.Id);//忽略ID字段
                config.Bind(x => x.Name, y => y.UserName);//将源类型和目标类型的字段对应绑定起来
                config.Bind(x => x.Age, y => y.Age);//将源类型和目标类型的字段对应绑定起来
            });
            var person = new Person
            {
                Id = Guid.NewGuid().ToString(),
                Name = "John",
                Age = 22
            };
            var personDto = TinyMapper.Map<PersonDto>(person);
            Console.WriteLine("Id:{0},Name:{1},Age:{2}", personDto.Id, personDto.UserName, personDto.Age);
            Console.ReadLine();
        }
    }

    public class Person
    {
        public String Id { get; set; }
        public String Name { get; set; }
        public Int32 Age { get; set; }
    }
    public class PersonDto
    {
        public String Id { get; set; }
        public String UserName { get; set; }
        public Int32 Age { get; set; }
    }
}

3.Tiny Mapper复杂类型使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nelibur.ObjectMapper;
using Nelibur.ObjectMapper.Bindings;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            TinyMapper.Bind<Person, PersonDto>(config =>
            {
                config.Ignore(x => x.Id);//忽略ID字段

                //将源类型和目标类型的字段对应绑定起来
                config.Bind(x => x.Name, y => y.UserName);
                config.Bind(x => x.Age, y => y.Age);
                config.Bind(x => x.Address, y => y.Address);
                config.Bind(x => x.Emails, y => y.Emails);
            });
            var person = new Person
            {
                Id = Guid.NewGuid().ToString(),
                Name = "John",
                Age = 22,
                Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" },
                Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" }
            };
            var personDto = TinyMapper.Map<PersonDto>(person);
        }
    }
    public class Person
    {
        public String Id { get; set; }
        public String Name { get; set; }
        public Int32 Age { get; set; }
        public Address Address { get; set; }
        public List<String> Emails { get; set; }
    }
    public class PersonDto
    {
        public String Id { get; set; }
        public String UserName { get; set; }
        public Int32 Age { get; set; }
        public Address Address { get; set; }
        public List<String> Emails { get; set; }
    }
    public sealed class Address
    {
        public string Phone { get; set; }
        public string Street { get; set; }
        public string ZipCode { get; set; }
    }
}

 

相关文章:

  • 2021-06-16
  • 2022-12-23
  • 2022-03-05
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-04-08
  • 2021-04-03
  • 2021-06-12
  • 2021-12-18
相关资源
相似解决方案