zoukankan      html  css  js  c++  java
  • .Net Mvc AutoMapper简单使用

    1、安装automapper nuget包。

    2、新建一个AutoMapper配置类并实现一个静态配置方法。

    方法一、

    using AutoMapper;
    using AutoMapperTest.Models;
    
    namespace AutoMapperTest.App_Start
    {
        public class AutoMapperConfig
        {
            public static void Config()
            {
                Mapper.Initialize(cfg =>
                {
                    cfg.CreateMap<StudentEntity, StudentOutput>();
                });
            }
        }
    }

    方法二、AddProfile方式

    using AutoMapper;
    using AutoMapperTest.Models;
    
    namespace AutoMapperTest.App_Start
    {
        public class AutoMapperConfig
        {
            public static void Config()
            {
                Mapper.Initialize(cfg =>
                {
                    cfg.AddProfile<MapperProfile>();
                });
            }
        }
    }
    using AutoMapper;
    using AutoMapperTest.Models;
    
    namespace AutoMapperTest.App_Start
    {
        public class MapperProfile : Profile
        {
            public MapperProfile()
            {
                CreateMap<StudentEntity, StudentOutput>();
            }
        }
    }

    3、在全局配置Global.asax中引用配置方法。

    using AutoMapperTest.App_Start;
    using System.Web.Mvc;
    using System.Web.Optimization;
    using System.Web.Routing;
    
    namespace AutoMapperTest
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AutoMapperConfig.Config();
            }
        }
    }

    4、具体使用

            public JsonResult GetMapper()
            {
                //实例化实体List
                List<StudentEntity> StudentList = new List<StudentEntity>();
                //模拟数据
                StudentList.Add(new StudentEntity
                {
                    Id = 1,
                    Age = 12,
                    Gander = "boy",
                    Name = "WangZeLing",
                    Say = "Only the paranoid survive",
                    Score = 99M
                });
                //AuotMapper具体使用方法 将List<StudentOutput>转换为List<StudentOutput>
                List<StudentOutput> Output = AutoMapper.Mapper.Map<List<StudentOutput>>(StudentList);
                return Json(Output, JsonRequestBehavior.AllowGet);
            }

    附:实体类、Output类

        public class StudentEntity
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
            public string Gander { get; set; }
            public decimal Score { get; set; }
            public string Say { get; set; }
        }
        public class StudentOutput
        {
            public string Name { get; set; }
            public decimal Score { get; set; }
            public string Say { get; set; }
        }

    附:AutoMapper GitHub 

    https://github.com/AutoMapper/AutoMapper
  • 相关阅读:
    一夜风雨,夏入秋
    Starting,博客园的开通,渐行渐远
    The second day
    The first day study
    [前缀和] Jzoj P3913 艰难的选择
    [树形dp] Jzoj P3914 人品问题
    [匈牙利][Floyd] Jzoj P1015 导弹
    [高精度][规律][二分] Jzoj P4213 对你的爱深不见底
    [概率dp] Jzoj P4212 我想大声告诉你
    [最小割][最大流] 洛谷 P1345 奶牛的电信
  • 原文地址:https://www.cnblogs.com/eedc/p/6857742.html
Copyright © 2011-2022 走看看