Don't Create Your Own Mapper, Let AutoMapper Do The Job!

Mapping is a common task when integrating different systems with each other. Although it might often seem an unnecessary overhead, it’s highly suggested (even when you communicate within your own systems) as you don’t want to depend on the internal structure of another service. I’m talking about DTOs basically. But please, don’t create your own mapper, there are tools out there which solve this task probably better than you might.

I’ve implemented my custom mappers as well. When you do so I highly suggest you to write some automated tests! They’ll save your life the next time your object structure changes. But at the same time such tests are extremely tedious to write and moreover they are short-lived. Why? Because they often look like

You have to compare property by property. Now this is a simple and plain object structure, but I guess you can easily imagine how quickly this mapping becomes more complex once you have nested subobjects and lists with different structures. I can assure you’ll have to write dozens of tests just to cover the different edge cases. The short-lived nature of such tests comes from the simple fact that they are tightly coupled to the underlying object structure and that’s actually correct as they test it.

Testing

The AssertConfigurationIsValid() will throw a nice, expressive exception in case it fails. Note that in the configuration mapping you also have to specify eventual properties you don’t like to be mapped:

ASP.net MVC DTOs

Whether you apply the MVVM pattern or you create a REST-like API for JavaScript clients. One of my lessons learned is for sure to not expose the Entity Framework entities directly. Instead it is preferrable to create specific DTOs (or you could call them ViewModels as well) where you create highly specialized entities just of the purpose you need.

A useful feature in such case is AutoMapper’s Flattening. Basically instead of rendering an object graph you often prefer to transfer a flat object to your client as it is easier to bind on the UI. So if you have an Order with a nested Customer object like

class Order
int Id
Customer TheCustomer
string Name

and you’d like to display the order information together with the customer’s name on your UI then you can create a DTO

class OrderDto
int Id
string TheCustomerName

and a mapping configuration

Mapper.CreateMap<Order, OrderDto>();

The interesting property here is the TheCustomerName property in the OrderDto. AutoMapper basically fills it following the source object’s relation TheCustomer and taking the corresponding Name property. Extremley elegant!

To come back to ASP.net MVC. You could now invoke the DtoMapper.Init() where appropriate (i.e. in the Global.asax or the controller constructor) for initializing the mapping configurations. The controller method then looks as follows

In Java?

Its quite some time since I last actively developed in Java, but apparently there is a new tool called ModelMapper which does quite a similar job as AutoMapper. You may want to check it out here: http://modelmapper.org/.