首页 > 代码库 > AutoMapper

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