Mapper.Initialize(cfg => {cfg.CreateMap<Order,OrderListViewModel>();}
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());
});
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();
领域模型中类名+属性映射为属性,类名+Get+方法名映射为属性
建类全局的类型映射关系
config.AssertConfigurationIsValid();
.ForMember(dest => dest.Total, opt => opt.ResolveUsing()));