1

Closed

Mapping EFcomplex Entities into WCF DataContracts using AutoMapper

description

I'm new to AutoMapper and i have the following manual mapping in CustomerService in Service Layer in my application, where i tried to find out the equivalent AutoMapper mapping code but i couldn't due to nested and complex objects and collections, so please help me to resolve this issue

here the code from CustomerService : Service.Customer is the DataContract and Data.Customer is the Entity

public Service.Customer GetCustomer(string customerID)
{
    Data.Customer customer
        = _northwindEntities
            .Customers.Single(
                c => c.CustomerID == customerID);
    return new Service.Customer
               {
                   CustomerID = customer.CustomerID,
                   CompanyName = customer.CompanyName,
                   ContactName = customer.ContactName,
                   Address = customer.Address,
                   City = customer.City,
                   Country = customer.Country,
                   Region = customer.Region,
                   PostalCode = customer.PostalCode,
                   Phone = customer.Phone,
                   Orders 
                    = GetOrders(customer.Orders)
               };
}
Service.Order is the DataContract and Data.Order is the Entity ...

private static IEnumerable<Service.Order> GetOrders(
    IEnumerable<Data.Order> order)
{
    return order.Select(o => new Service.Order
        {
            OrderID = o.OrderID,
            OrderDate = o.OrderDate,
            OrderDetails = GetOrderDetails(o),
            Freight = o.Freight,
            ShippedDate = o.ShippedDate
        }).ToList();
}
Service.OrderDetail is the DataContract and Data.Order is the Entity ...

private static IEnumerable<Service.OrderDetail> GetOrderDetails(
    Data.Order order)
{

    return order.Order_Details.Select(
        o => new Service.OrderDetail
        {
            Product 
                = new Service.Product
                {
                    ProductID 
                        = o.Product.ProductID,
                    ProductName 
                        = o.Product.ProductName
                },
            Quantity = o.Quantity,
            UnitPrice = o.UnitPrice
        }).ToList();
}
So OrderDetails are nested inside Oreders and Orders are nested inside Customer ...How to reflect this hierarchy using AutoMapper Thanks in advance
Closed May 8 at 1:19 PM by jbogard

comments