对象映射组件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; }
    }
}

 

转载于:https://www.cnblogs.com/lgxlsm/p/5512852.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值