当前位置: 首页 > 工具软件 > Automapper.js > 使用案例 >

Automapper使用

涂羽
2023-12-01

示例

初始化

           Mapper.Initialize(cfg => {cfg.CreateMap<Order,OrderListViewModel>();}

Profile

public class Order
 {
        public decimal Amount { get; set; }
 }

 public class OrderListViewModel
 {
      public string Amount { get; set; }
 }
public class ViewModelProfile : Profile
{
    public ViewModelProfile()
    {
        CreateMap<Order, OrderListViewModel>();
        CreateMap<decimal, string>().ConvertUsing(value => value.ToString("c"));
   }
}

var config = new MapperConfiguration(cfg =>
{
     cfg.AddProfile<ViewModelProfile>();
});

数组

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<Source, Dest>();
});
var destArray = config.CreateMapper().Map<Source[], Dest[]>(sourceArray);

类内部数组

       public class Order
        {
            private IList<OrderLine> _lineItems = new List<OrderLine>();

            public OrderLine[] LineItems { get { return _lineItems.ToArray(); } }

            public void AddLineItem(OrderLine orderLine)
            {
                _lineItems.Add(orderLine);
            }
        }

        public class OrderLine
        {
            public int Quantity { get; set; }
        }

        public class OrderDto
        {
            public OrderLineDto[] LineItems { get; set; }
        }

        public class OrderLineDto
        {
            public int Quantity { get; set; }
        }

            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<Order, OrderDto>();
                cfg.CreateMap<OrderLine, OrderLineDto>();
            });

继承类

子类转换为父类不需要创建映射关系

字典映射

     var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<SourceValue, DestValue>();
            });

var destDict = config.CreateMapper().Map

枚举映射

 public enum OrderStatus : short
 {
     InProgress = 0,
     Complete = 1
 }

 //枚举之间互相映射
  mapper.Map<OrderStatus, OrderStatusDto>(OrderStatus.InProgress).ShouldEqual(OrderStatusDto.InProgress);
  //枚举转换为数字
  mapper.Map<OrderStatus, short>(OrderStatus.Complete).ShouldEqual((short)1);
  //枚举转换为字符串
  mapper.Map<OrderStatus, string>(OrderStatus.Complete).ShouldEqual("Complete");
  mapper.Map<short, OrderStatus>(1).ShouldEqual(OrderStatus.Complete);
  mapper.Map<string, OrderStatus>("Complete").ShouldEqual(OrderStatus.Complete);

设置映射字段之间的关系

  var config = new MapperConfiguration(cfg =>
   {
       cfg.CreateMap<Source, Destination>()
                    .ForMember(d => d.DestValue, opt => opt.MapFrom(s => s.SrcValue));
   });

接口映射

类名+属性名映射为响应的字段
public class OrderForm
{
public Customer Customer { get; set; }
}

        public class Customer
        {
            public string Name { get; set; }
        }

        public interface ICreateOrderMessage
        {
            string CustomerName { get; set; }
        }

 var message = mapper.Map<OrderForm, ICreateOrderMessage>(order);

忽略某些属性

                 var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap<OrderDto, Order>()
                        .ForMember(dest => dest.Id, opt => opt.Ignore());
                });

CustomResolver

        public class Source
        {
            public int Value1 { get; set; }
            public int Value2 { get; set; }
        }

        public class Destination
        {
            public int Total { get; set; }
        }

        public class CustomResolver : IValueResolver<Source, Destination, int>
        {
            public int Resolve(Source source, Destination d, int dest, ResolutionContext context)
            {
                return source.Value1 + source.Value2;
            }
        }
         var config = new MapperConfiguration(cfg => cfg.CreateMap<Source, Destination>()
                .ForMember(dest => dest.Total, opt => opt.ResolveUsing<CustomResolver>()));
            config.AssertConfigurationIsValid();

Flattening

领域模型中类名+属性映射为属性,类名+Get+方法名映射为属性

全局映射

建类全局的类型映射关系

类的常用属性

config.AssertConfigurationIsValid();
.ForMember(dest => dest.Total, opt => opt.ResolveUsing()));

 类似资料: