Donate

Using AutoMapper In Asp.Net Web Forms 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 by incorporating the tool in an ASP.NET WebForms application. Here's the code snippets breakdown:
Order class:
/// <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:
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:
//Bootstrapper class  
 public class BootStrapper  
   {  
     public static void ConfigureAutoMapper()  
     {  
       Mapper.CreateMap<Customer, CustomerView>();  
       Mapper.CreateMap<Order, OrderView>();  
       Mapper.CreateMap<OrderDetails, OrderDetailsView>();  
     }  
   }  
Global.asax:
//Global.asax 
void Application_Start(object sender, EventArgs e) 
{ 
   // Code that runs on application startup       
   BootStrapper.ConfigureAutoMapper(); 
} 
OrderExtension class:
public static class OrderExtensionMethods  
  {  
    public static OrderView ConvertToOrderView(this Order order)  
    {  
      return Mapper.Map<Order, OrderView>(order);  
    }  
  }  
Order Service class:
//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:
/// <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:
Using AutoMapper In Asp.Net Web Forms Database Application
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!

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.

Invalid nested tag div found, expected closing tag input