Thursday, November 14, 2013

Using AutoMapper in Asp.Net Database Application

Hello,
Based from Scott Millet's Asp.Net Design Patterns, I was curious on the AutoMapper Framework that maps Domain Entities to View Models. So I tried to give it a spin. Here's the code snippets breakdown:
Order class:
Code:
   /// <summary>  
   /// One order may contain one or many order details.  
   /// </summary>  
   public class Order  
   {  
     public int OrderId { get; set; }  
     public DateTime OrderDate { get; set; }  
     public DateTime RequiredDate { get; set; }  
     public string ShipName { get; set; }  
     public string ShipAddress { get; set; }  
     public IList<OrderDetails> OrderDetail { get; set; }  
   }
Order View class:
Code:
 public class OrderView  
   {  
     public int OrderId { get; set; }  
     public DateTime OrderDate { get; set; }  
     public DateTime RequiredDate { get; set; }  
     public string ShipName { get; set; }  
     public string ShipAddress { get; set; }  
     public IList<OrderDetails> OrderDetail { get; set; }  
   }  
Bootstrapper class:
Code:
//Bootstrapper class  
 public class BootStrapper  
   {  
     public static void ConfigureAutoMapper()  
     {  
       Mapper.CreateMap<Customer, CustomerView>();  
       Mapper.CreateMap<Order, OrderView>();  
       Mapper.CreateMap<OrderDetails, OrderDetailsView>();  
     }  
   }  
Global.asax:
Code:
//Global.asax 
void Application_Start(object sender, EventArgs e) 
{ 
   // Code that runs on application startup       
   BootStrapper.ConfigureAutoMapper(); 
} 
OrderExtension class:
Code:
public static class OrderExtensionMethods  
  {  
    public static OrderView ConvertToOrderView(this Order order)  
    {  
      return Mapper.Map<Order, OrderView>(order);  
    }  
  }  
Order Service class:
Code:
//OrderService class method  
 public CustomerView GetAllOrders(string Id)  
 {  
      CustomerView customerView;  
      ModelCustomer customer = new ModelCustomer();  
      customer.Orders = _orderRepository.FindAllOrders(Id);  
      customerView = customer.ConvertToCustomerView();  
      return customerView;  
 }  
Order Repository class:
Code:
/// <summary>  
     /// Repository class  
     /// Search orders by customer id.  
     /// </summary>  
     public IList<Model.Order> FindAllOrders(string Id)  
     {  
       var Orders = from _order in context.Orders  
              where _order.CustomerID == Id  
              select _order;  
       foreach (var item in Orders)  
           {  
         orders.Add(new ModelOrder()  
         {  
           OrderId = item.OrderID,  
           RequiredDate = Convert.ToDateTime(item.RequiredDate),  
           ShipAddress = item.ShipAddress,  
           ShipName = item.ShipName,  
           OrderDate = Convert.ToDateTime(item.OrderDate),  
           //get details per order  
           OrderDetail = FindAllOrderDetails(item.OrderID)   
         });  
           }  
       return (IList<Model.Order>)orders;  
     }  
Running asp.net website:
As you noticed from the image above, a customer's placed orders
are shown in the first gridview. While the order details of a
specific order is displayed on the second grid.

The asp.net sample focused on the Northwind database.
This application will retrieve all customers and their placed
orders. Additionally, this will retrieve the order details for
a specific order.

Basically, I just derived the concept from Scott's sample and
translatting it to a database project. The changes made for this
application is minimal.

Cheers!

0 comments:

Post a Comment