AutoMapper

项目地址:AutoMapper

1.Flattening

配置器尝试匹配源类型中的属性和方法到目的类型,对于源类型中以"Get"为前缀的方法或属性,AutoMapper会按照驼峰命名格式分割这个属性或方法并映射到对应的目的类型中

AutoMapper
 1  public class Order

 2     {

 3         private readonly IList<OrderLineItem> _orderLineItems = new List<OrderLineItem>();

 4 

 5         public Customer Customer { get; set; }

 6 

 7         public OrderLineItem[] GetOrderLineItems()

 8         {

 9             return _orderLineItems.ToArray();

10         }

11 

12         public void AddOrderLineItem(Product product, int quantity)

13         {

14             _orderLineItems.Add(new OrderLineItem(product, quantity));

15         }

16 

17         public decimal GetTotal()

18         {

19             return _orderLineItems.Sum(li => li.GetTotal());

20         }

21     }

22 

23     public class Product

24     {

25         public decimal Price { get; set; }

26         public string Name { get; set; }

27     }

28 

29     public class OrderLineItem

30     {

31         public OrderLineItem(Product product, int quantity)

32         {

33             Product = product;

34             Quantity = quantity;

35         }

36 

37         public Product Product { get; private set; }

38         public int Quantity { get; private set;}

39 

40         public decimal GetTotal()

41         {

42             return Quantity*Product.Price;

43         }

44     }

45 

46     public class Customer

47     {

48         public string Name { get; set; }

49     }

50 

51  public class OrderDto

52     {

53         public string CustomerName { get; set; }

54         public decimal Total { get; set; }

55     }

56 

57  var customer = new Customer

58         {

59             Name = "George Costanza"

60         };

61     var order = new Order

62         {

63             Customer = customer

64         };

65     var bosco = new Product

66         {

67             Name = "Bosco",

68             Price = 4.99m

69         };

70     order.AddOrderLineItem(bosco, 15);

71 

72     // 配置AutoMapper

73 

74     Mapper.CreateMap<Order, OrderDto>();

75 

76     // 执行 mapping

77 

78     OrderDto dto = Mapper.Map<Order, OrderDto>(order);

79 

80     dto.CustomerName.ShouldEqual("George Costanza");

81     dto.Total.ShouldEqual(74.85m);
View Code

2.Projection(映射)

AutoMapper
 1  public class CalendarEvent

 2     {

 3         public DateTime Date { get; set; }

 4         public string Title { get; set; }

 5     }

 6 

 7  public class CalendarEventForm

 8     {

 9         public DateTime EventDate { get; set; }

10         public int EventHour { get; set; }

11         public int EventMinute { get; set; }

12         public string Title { get; set; }

13     }

14 

15  // Model

16     var calendarEvent = new CalendarEvent

17         {

18             Date = new DateTime(2008, 12, 15, 20, 30, 0),

19             Title = "Company Holiday Party"

20         };

21 

22     // 配置AutoMapper

23     Mapper.CreateMap<CalendarEvent, CalendarEventForm>()

24         .ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.Date.Date))

25         .ForMember(dest => dest.EventHour, opt => opt.MapFrom(src => src.Date.Hour))

26         .ForMember(dest => dest.EventMinute, opt => opt.MapFrom(src => src.Date.Minute));

27 

28     // 执行mapping

29     CalendarEventForm form = Mapper.Map<CalendarEvent, CalendarEventForm>(calendarEvent);

30 

31     form.EventDate.ShouldEqual(new DateTime(2008, 12, 15));

32     form.EventHour.ShouldEqual(20);

33     form.EventMinute.ShouldEqual(30);

34     form.Title.ShouldEqual("Company Holiday Party");
View Code

 3.Configuration validation

AutoMapper
 1   public class Source

 2     {

 3         public int SomeValue { get; set; }

 4     }

 5 

 6     public class Destination

 7     {

 8         public int SomeValuefff { get; set; }

 9     }

10 

11    Mapper.CreateMap<Source, Destination>();

12 

13     Mapper.AssertConfigurationIsValid();
View Code

当目的类型中的属性与源类型中的不匹配是会抛出'AutoMapperConfigurationException'异常,通过以下方式解决:

a.重命名

b.配置映射(Projection)

 Mapper.CreateMap<Source, Destination>()

       .ForMember(a => a.SomeValuefff, b => b.MapFrom(c => c.SomeValue));

c.使用Ignore()选项

 Mapper.CreateMap<Source, Destination>()

       .ForMember(dest => dest.SomeValuefff, opt => opt.Ignore());

4.Lists and arrays

AutoMapper
 1 public class Source

 2     {

 3         public int Value { get; set; }

 4     }

 5 

 6     public class Destination

 7     {

 8         public int Value { get; set; }

 9     }

10 

11 Mapper.CreateMap<Source, Destination>();

12 

13     var sources = new[]

14         {

15             new Source { Value = 5 },

16             new Source { Value = 6 },

17             new Source { Value = 7 }

18         };

19 

20     IEnumerable<Destination> ienumerableDest = Mapper.Map<Source[], IEnumerable<Destination>>(sources);

21     ICollection<Destination> icollectionDest = Mapper.Map<Source[], ICollection<Destination>>(sources);

22     IList<Destination> ilistDest = Mapper.Map<Source[], IList<Destination>>(sources);

23     List<Destination> listDest = Mapper.Map<Source[], List<Destination>>(sources);

24     Destination[] arrayDest = Mapper.Map<Source[], Destination[]>(sources);
View Code

 

你可能感兴趣的:(mapper)