In Part 4 we will see how we can use projection to map inner objects for example a DateTime field. Again our domain and Dto (Data Transfer Object) looks like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
//Order public class Order { public string OrderNo { get; set; } public Customer Customer { get; set; } public DateTime PurchaseDate { get; set; } public IEnumerable<OrderItems> LineItems { get; set; } public bool ShipToHomeAddress { get; set; } public decimal GetTotal() { return LineItems == null ? 0 : LineItems.Sum(x => x.GetTotalPrice()); } public Guid InternalId { get; set; } } //Dto public class OrderDateDto { public int PurchaseHour { get; set; } public int PurchaseMinute { get; set; } public string CustomerName { get; set; } } |
We will map the PurchaseDate in the Domain Object to our Dto PurchaseHour and Purchase Minute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
public ActionResult OrderDate() { var order = _respository.Get(3); order.PurchaseDate = new DateTime(2011, 3, 15, 20, 30, 0); Mapper.CreateMap<Order, OrderDateDto>() .ForMember(dest => dest.PurchaseHour, opt => opt.MapFrom(src => src.PurchaseDate.Hour)) .ForMember(dest => dest.PurchaseMinute, opt => opt.MapFrom(src => src.PurchaseDate.Minute)); var model = Mapper.Map<Order, OrderDateDto>(order); return View(model); } //View <h2>OrderDate</h2> @Model.CustomerName @Model.PurchaseHour @Model.PurchaseMinute |
As you can see the View now […]