AutoMapper的初次试水

1.创建一个控制台

2.打开Nuget,安装  AutoMapper包

控制台代码:

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

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Patient pt = new Patient
            {
                Name = "ddd",
                Age = 14,
                Sex = ""
            };

            Mapper.Initialize(x=>x.CreateMap<Patient,TT>());

            TT t = Mapper.Map<Patient, TT>(pt);

            Console.WriteLine(t.Body);
            Console.WriteLine(t.Num);
            Console.WriteLine(t.Sum);
            Console.Read();

        }
    }
}

 

 

2.创建Model  Patient.cs、TT.cs

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

namespace Test.Models
{
    public class Patient
    {
        [MapTo("Body")]
        public string Name { get; set; }

        [MapTo("Num")]
        public int Age { get; set; }

        [MapTo("Sum")]
        public string Sex { get; set; }
    }

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

namespace Test.Models
{
    public class TT
    {
        public string Body { get; set; }

        public string Num { get; set; }

        public string Sum { get; set; }
    }
}

 

 

断点执行,注意 TT.cs   中  Num 的类型  和 Patient.cs   中 Age  的类型不一样,结果都能够转换。

 

感谢:https://zhidao.baidu.com/question/1607626619722621507.html

http://blog.csdn.net/csethcrm/article/details/52934325

 

补充:

如果转换的是List列表时候,需要初始化映射下,不然会报错(但是有个问题撒,由于Mapper.Initialize 只有第一次初始化使用才有效,再次使用会报错)

 var waitVisitInfo = _doctorBLL.GetCheckWaitQueue(param);

                if (waitVisitInfo.Count == 0)
                {
                    result.SetError("", "没有找到数据!");
                }
                else
                {
                    //初始化映射 和单个对象的映射一样
                    Mapper.Initialize(cfg => cfg.CreateMap<GetCheckWaitQueueById_Result, GetCheckWaitQueueOut>());

                    List<GetCheckWaitQueueOut> waitVisitList = Mapper.Map<List<GetCheckWaitQueueById_Result>, List<GetCheckWaitQueueOut>>(waitVisitInfo);

                    //开始转换
                    var data = (from c in waitVisitList
                                select new { item = c }).ToList();

                    if (waitVisitList.Count == 0)
                    {
                        result.SetError("", "没有找到数据!");
                    }
                    else
                    {
                        result.SetSuccess(data);
                    }
                }

 

所以,最后使用在 AutoMapperConfig.cs 中,配置 Mapper.Initialize的对象

public class AutoMapperConfig
    {
        /// <summary>
        /// 
        /// </summary>
        public static void Config() {
            Mapper.Initialize(m => {
                m.CreateMap<GetDeptById_Result, GetDeptListOut>();
                m.CreateMap<GetDoctorListById_Result, GetDoctorListOut>();
                m.CreateMap<GetCurrentRegById_Result, GetCurrentRegOut>();
                m.CreateMap<GetAppointRegStatisticsById_Result, GetAppointRegStatisticsOut>();
                m.CreateMap<GetAppointRegById_Result, GetAppointRegOut>();
                m.CreateMap<GetPendingRegQueryById_Result, GetPendingRegQueryOut>();
                m.CreateMap<GetWaitVisitQueueById_Result, GetWaitVisitQueueOut>();
                m.CreateMap<GetCheckWaitQueueById_Result, GetCheckWaitQueueOut>();
            });         
        }
    }

最后,需要在 Global.asax 中 Application_Start 配置 AutoMapperConfig.Config();

 

protected void Application_Start()
        {
          //  AutoMapperConfig.Config();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            AutoMapperConfig.Config();
        }

 

 

 

感谢:https://www.cnblogs.com/wuyunblog/p/6666485.html

 https://stackoverflow.com/questions/38999771/test-method-x-threw-exception-system-invalidoperationexception-mapper-not-in

转载于:https://www.cnblogs.com/dzw159/p/8528066.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值