AutoMapper

Posted 邢帅杰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AutoMapper相关的知识,希望对你有一定的参考价值。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using WebSiteUI.Dto;
using Domain;

namespace WebSiteUI
{
    public partial class AutoMapperConfiguration
    {
        /// <summary>
        /// 配置AutoMapper
        /// </summary>
        public static void Config()
        {
            Mapper.CreateMap<MenuDto, S_MENU>();
            Mapper.CreateMap<S_MENU, MenuDto>();

            Mapper.CreateMap<UserDto, S_USER>();
            Mapper.CreateMap<S_USER, UserDto>();
        }

        /// <summary>
        /// AutoMapper 自定义扩展配置
        /// </summary>
        public static void ConfigExt()
        {
            //Mapper.CreateMap<MenuDto, S_MENU>().ForMember(u => u.M_CODE, e => e.MapFrom(s => s.M_CODE));
            
        }
    }
}

global.asax.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace WebSiteUI
{
    public class MvcApplication : Spring.Web.Mvc.SpringMvcApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(Server.MapPath("~/log4net.config")));

            //AutoMapper
            AutoMapperConfiguration.Config();
            AutoMapperConfiguration.ConfigExt();
        }
    }
}

 

以上是关于AutoMapper的主要内容,如果未能解决你的问题,请参考以下文章

AutoMapper:在运行时读取 Mapper 配置?

使用 AutoMapper 自动在多个数据模型间进行转换

ASP.NET Core 中的对象映射之 AutoMapper

对象映射工具AutoMapper介绍

对象到对象映射-AutoMapper

ioc初步理解 简单实用autofac搭建mvc三层+automapper=》ioc(codeFirst)