zoukankan      html  css  js  c++  java
  • 对象映射组件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; }
        }
    }
  • 相关阅读:
    如何查看linux端口被哪个进程占用
    Beego 结合 GORM 操作 Mysql 数据库
    Linux Go proxy 设置
    working directory is not part of a module
    依赖注入 gin项目的目录结构说明
    详解django中使用定时任务的方法
    input 原声上传文件 file转化为binary对象发送给后台
    vue篇之事件总线(EventBus)
    小程序路由遇到的问题(eventChannel.emit is not a function报错)
    小程序组件(弹窗组件以及插槽使用)
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/5512852.html
Copyright © 2011-2022 走看看