Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's Automapper for?

Tags:

What’s Automapper for?

How will it help me with my domain and controller layers (asp.net mvc)?

like image 545
Quintin Par Avatar asked Jan 19 '10 03:01

Quintin Par


People also ask

When should I use AutoMapper?

Use AutoMapper to eliminate the need to write tedious boilerplate code when mapping objects in your application. AutoMapper is a popular object-to-object mapping library that can be used to map objects belonging to dissimilar types.

What is the use of AutoMapper in .NET core?

AutoMapper is a simple library that helps us to transform one object type into another. It is a convention-based object-to-object mapper that requires very little configuration. The object-to-object mapping works by transforming an input object of one type into an output object of a different type.

What is AutoMapper in MVC?

AutoMapper is an object-object mapper that allows you to solve the problem of manually mapping each property of a class with the same properties of another class. Before AutoMapper was introduced if we wanted to assign one object property to another object property then we were following a long procedure.

Is it worth using AutoMapper?

As I mentioned above, the main advantage of AutoMapper is that it saves time spent on writing the mapping code. I got this question: “Do you really write mapping code for all 50+ fields of a type?” And the answer is simple.


1 Answers

Maybe an example will help here...

Let's say you have a nicely-normalized database schema like this:

Orders       (OrderID, CustomerID, OrderDate)  
Customers    (CustomerID, Name)  
OrderDetails (OrderDetID, OrderID, ProductID, Qty)  
Products     (ProductID, ProductName, UnitPrice)  

And let's say you're using a nice O/R mapper that hands you back a well-organized domain model:

OrderDetail
+--ID
+--Order
|--+--Date
|--+--Customer
|-----+--ID
|-----+--Name
+--Product
|--+--ID
|--+--Name
|--+--UnitPrice
+--Qty

Now you're given a requirement to display everything that's been ordered in the last month. You want to bind this to a flat grid, so you dutifully write a flat class to bind:

public class OrderDetailDto
{
    public int ID { get; set; }
    public DateTime OrderDate { get; set; }
    public int OrderCustomerID { get; set; }
    public string OrderCustomerName { get; set; }
    public int ProductID { get; set; }
    public string ProductName { get; set; }
    public Decimal ProductUnitPrice { get; set; }
    public int Qty { get; set; }

    public Decimal TotalPrice
    {
        get { return ProductUnitPrice * Qty; }
    }
}

That was pretty painless so far, but what now? How do we turn a bunch of OrderDetails into a bunch of OrderDetailDtos for data binding?

You might put a constructor on OrderDto that takes an OrderDetail, and write a big mess of mapping code. Or you might have a static conversion class somewhere. Or, you could use AutoMapper, and write this instead:

Mapper.CreateMap<OrderDetail, OrderDetailDto>();
OrderDetailDto[] items =
    Mapper.Map<OrderDetail[], OrderDetailDto[]>(orderDetails);
GridView1.DataSource = items;

There. We've just taken what would otherwise have been a disgusting mess of pointless mapping code and reduced it into three lines (really just two for the actual mapping).

Does that help explain the purpose?

like image 56
Aaronaught Avatar answered Oct 08 '22 00:10

Aaronaught