官网:https://docs.automapper.org/en/stable/Getting-started.html#how-do-i-use-automapper
参考:https://www.cnblogs.com/wl-blog/p/16403607.html

using AutoMapper;

namespace XCGWebApp.Common
{
    public class Utils
    {
        /// <summary>
        /// 把F类型的对象map成T类型的对象
        /// </summary>
        /// <typeparam name="F"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="from"></param>
        /// <returns></returns>
        public static T MapTo<F, T>(F from)
        {
            // 转换
            var config = new MapperConfiguration(cfg => cfg.CreateMap<F, T>());
            var mapper = new Mapper(config);
            var mapRes = mapper.Map<T>(from);
            return mapRes;
        }
    }
}

最新版的10.1.1,跟以前的用法不一样了

using AutoMapper;
using FMS.Dto;
using FMS.FMSDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FMS.utils
{
    public class AutoMapperConfig
    {
        //var mapper = config.CreateMapper();
        // or
        //var mapper = new Mapper(config);
        public static Mapper Config()
        {
            return new Mapper(new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<User, UserDto>();
            }));
        }
    }
}

var mapper = AutoMapperConfig.Config(); 
List<UserDto> dtos = mapper.Map<List<UserDto>>(userList);

旧版代码:

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();
        }
    }
}

 

posted on 2017-05-31 15:30  邢帅杰  阅读(169)  评论(0编辑  收藏  举报