文章来源:https://www.toymoban.com/news/detail-627107.html
using AutoMapper;
using Zrj.TryUseAutoMapper.Model.Entity;
using Zrj.TryUseAutoMapper.Model.DTO;
using Zrj.TryUseAutoMapper.Model.CustomerProfile;
namespace TryUseAutoMapper
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
#region 概念
/*
* 为什么使用AutoMapper
* 当后端的实体类/模型和前端要求有出入/有些字段不方便带到前端时,解耦分离
*/
#endregion
/*当只转换少数类的写法*/
{
var mapperConfiguration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Orders, DTO_Orders>();
});
var mapper = mapperConfiguration.CreateMapper(); /*等同于*/ //IMapper mapper = mapperConfiguration.CreateMapper();
DTO_Orders orders = mapper.Map<Orders, DTO_Orders>(new Orders()
{
CustomerId = 1,
CustomerEmail = "*****@163.com",
OrderMoney = 1_000_000
});
/*
* 无法获取到 orders.OrderMoney
*/
Console.WriteLine(orders.CustomerEmail);
}
/*
* 当转换部分的写法
*/
{
var mapperConfiguration = new MapperConfiguration(cfg =>
{
//cfg.CreateMap<Orders, DTO_Orders>();
cfg.AddProfile<CustomerProfile>();
});
var mapper = mapperConfiguration.CreateMapper(); /*等同于*/ //IMapper mapper = mapperConfiguration.CreateMapper();
DTO_Orders orders = mapper.Map<Orders, DTO_Orders>(new Orders()
{
CustomerId = 1,
CustomerEmail = "*****@163.com",
OrderMoney = 1_000_000
});
/*
* 无法获取到 orders.OrderMoney
*/
Console.WriteLine(orders.CustomerEmail);
}
Console.ReadKey();
}
}
}
类库
文章来源地址https://www.toymoban.com/news/detail-627107.html
namespace Zrj.TryUseAutoMapper.Model.Entity
{
public class Orders
{
public string Order { get; set; } = string.Empty;
public int CustomerId { get; set; }
public string CustomerName { get; set; } = string.Empty;
public string CustomerEmail { get; set; } = string.Empty;
public string CustomerPhone { get; set; } = string.Empty;
public string CustomerCity { get; set; } = string.Empty;
public string CustomerRegion { get; set; } = string.Empty;
public string CustomerPostalCode { get; set; } = string.Empty;
public string CustomerCountry { get; set; } = string.Empty;
public string CustomerState { get; set; } = string.Empty;
public decimal OrderMoney { get; set; }
}
}
namespace Zrj.TryUseAutoMapper.Model.DTO
{
public class DTO_Orders
{
public string Order { get; set; } = string.Empty;
public string CustomerId { get; set; } = string.Empty;
public string CustomerName { get; set; } = string.Empty;
public string CustomerEmail { get; set; } = string.Empty;
public string CustomerPhone { get; set; } = string.Empty;
public string CustomerCity { get; set; } = string.Empty;
public string CustomerRegion { get; set; } = string.Empty;
public string CustomerPostalCode { get; set; } = string.Empty;
public string CustomerCountry { get; set; } = string.Empty;
public string CustomerState { get; set; } = string.Empty;
}
}
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zrj.TryUseAutoMapper.Model.DTO;
using Zrj.TryUseAutoMapper.Model.Entity;
namespace Zrj.TryUseAutoMapper.Model.CustomerProfile
{
public class CustomerProfile : Profile
{
public CustomerProfile()
{
base.CreateMap<Orders, DTO_Orders>();
/*以此类推*/
//base.CreateMap<Orders, DTO_Orders>();
}
}
}
到了这里,关于为什么使用AutoMapper的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!