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:
Order View class:
Bootstrapper class:
Global.asax:
OrderExtension class:
Order Service class:
Order Repository class:
Running asp.net website:
Cheers!
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; } }
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 public class BootStrapper { public static void ConfigureAutoMapper() { Mapper.CreateMap<Customer, CustomerView>(); Mapper.CreateMap<Order, OrderView>(); Mapper.CreateMap<OrderDetails, OrderDetailsView>(); } }
//Global.asax void Application_Start(object sender, EventArgs e) { // Code that runs on application startup BootStrapper.ConfigureAutoMapper(); }
public static class OrderExtensionMethods { public static OrderView ConvertToOrderView(this Order order) { return Mapper.Map<Order, OrderView>(order); } }
//OrderService class method public CustomerView GetAllOrders(string Id) { CustomerView customerView; ModelCustomer customer = new ModelCustomer(); customer.Orders = _orderRepository.FindAllOrders(Id); customerView = customer.ConvertToCustomerView(); return customerView; }
/// <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; }
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.
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
Post a Comment