zoukankan      html  css  js  c++  java
  • AutoMapper,对象映射的简单使用

    using AutoMapper;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyMapperTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                Action<IMapperConfigurationExpression> configurationExpress = createMapperConfigurationExpress();
                //Mapper.Initialize(c => configurationExpress(c));
                //以下代码和第二行的Mapper.Initialize(c => configurationExpress(c));作用相同
                Mapper.Initialize(cfg => {
                    cfg.CreateMap<AddressDto, Address>();
                    cfg.CreateMap<Address, AddressDto>();
                });
    
                //单个映射实例
                //Mapper.Initialize(p => p.CreateMap<AddressDto, Address>());
                AddressDto dto = new AddressDto
                {
                    Country = "China",
                    City = "ShangHai",
                    Street = "JinZhong Street"
                };
                Address address = Mapper.Map<AddressDto, Address>(dto);
    
                AddressDto dto2 = Mapper.Map<Address, AddressDto>(address);
            }
    
            public static Action<IMapperConfigurationExpression> createMapperConfigurationExpress()
            {
                return new Action<IMapperConfigurationExpression>(cfg =>
                    {
                        cfg.CreateMap<AddressDto, Address>();
                        cfg.CreateMap<Address, AddressDto>();
                    }
                );
            }
        }
    
        public class AddressDto
        {
            public string Country { get; set; }
            public string City { get; set; }
            public string Street { get; set; }
        }
    
        public class Address
        {
            public string Country { get; set; }
            public string City { get; set; }
            public string Street { get; set; }
        }
    }
  • 相关阅读:
    NHbiernate 配置
    NHibernate开发入门
    Thread 线程简单例子
    C#中委托和事件
    DataGridView 去掉多余的列
    ASP.NET C# 有程序集加不了解决办法
    oracle“记录被另一个用户锁住”
    Android 控件属性
    Android 入门
    MVC 视频笔记
  • 原文地址:https://www.cnblogs.com/itjeff/p/9869364.html
Copyright © 2011-2022 走看看