AutoMapper的使用

这里仅仅是一些简单的使用实例。

usage:

using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class Source
    {
        public int SomeValue { get; set; }

        public string Anothervalue { get; set; }
    }

    public class Destination
    {
        public int SomeValue { get; set; }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            //先要创建一个map
            Mapper.CreateMap<Source, Destination>();        
            Source s = new Source() { SomeValue=1,Anothervalue="2" };
            Destination d = Mapper.Map<Destination>(s);
            Console.WriteLine(d.SomeValue);
            Console.ReadLine();
            

        }
    }
}

此外:我们可以再Profile中重写Configure方法,从而完成映射规则的配置。从Profile初始化Mapper规则:

using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class Source
    {
        public int SomeValue { get; set; }

        public string Anothervalue { get; set; }
    }

    public class Destination
    {
        public int SomeValue { get; set; }
    }
    public class ConfigInfo : Profile {
        protected override void Configure()
        {
            //添加需要映射的源和目标;
            Mapper.CreateMap<Source, Destination>(); 
        }
    }

 
    class Program
    {
        static void Main(string[] args)
        {
            //然后,在这里进行初始化滴呀;
            Mapper.Initialize(x => x.AddProfile<ConfigInfo>());   
            Source s = new Source() { SomeValue=1,Anothervalue="2" };
            Destination d = Mapper.Map<Destination>(s);
            Console.WriteLine(d.SomeValue);
            Console.ReadLine();
            

        }
    }
}

我们也可以指定映射的各个字段;

using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class Source
    {
        public int SomeValue { get; set; }

        public string Anothervalue { get; set; }
    }

    public class Destination
    {
        public int SomeValue { get; set; }
    }

    public class Destination2
    {
        public int SomeValue { get; set; }
        public string AnotherValue2 { get; set; }
    }
    public class ConfigInfo : Profile {
        protected override void Configure()
        {
            //添加需要映射的源和目标;
            Mapper.CreateMap<Source, Destination>();

            //我们这里还可以添加一些较为 复杂的约定;
            CreateMap<Source, Destination2>();
            //映射的前提是,我们的源和目标的字段,必须相同,如果不同,则我们无法映射;
            //但是我们可以 这里添加如下的方法 进行执行行 的映射滴呀

            // Anothervalue--AnotherValue2

            //CreateMap<Source, Destination2>().ForMember(d => d.AnotherValue2, opt =>
            //{
            //    opt.MapFrom(s => s.Anothervalue);
            //});
        }
    }

 
    class Program
    {
        static void Main(string[] args)
        {
            //然后,在这里进行初始化滴呀;
            Mapper.Initialize(x => x.AddProfile<ConfigInfo>());   
            Source s = new Source() { SomeValue=1,Anothervalue="2" };
            Destination d = Mapper.Map<Destination>(s);
            Destination2 d2 = Mapper.Map<Destination2>(s);
            Console.WriteLine(d.SomeValue);
            Console.WriteLine(d2.SomeValue+"--"+d2.AnotherValue2);
            Console.ReadLine();
            

        }
    }
}

 AutoMapper最佳实践

 在实际的项目中,我们会有很多类进行映射(从Entity转换为Dto,或者从Entity转换为ViewModel等

 首先我们需要定义一个Configuration.cs的类,该类提供AutoMapper规则配置的入口,它只提供一个静态的方法,在程序第一次运行的时候调用该方法完成配置。

Configuration为我们的静态配置入口类;Profiles文件夹为我们所有Profile类的文件夹。如果是MVC,我们需要在Global中调用:

AutoMapper.Configuration.Configure();
原文地址:https://www.cnblogs.com/mc67/p/5740186.html