Effortlessly map domain and DTO entities

I recently created a set of objects to complement the domain entities we use for our data access layer for a new project we’re working on.  I immediately set out to write a mapping utility.  After a bit of thought pondering the complexity of the problem I decided to look around on the magical internets to see what options were available.

Came across .   This is a perfect match for my needs:

AutoMapper uses a fluent configuration API to define an object-object mapping strategy.   AutoMapper uses a convention-based matching algorithm to match up source to destination values. Currently, AutoMapper is geared towards model projection scenarios to flatten complex object models to DTOs and other simple objects, whose design is better suited for serialization, communication, messaging, or simply an anti-corruption layer between the domain and application layer.

It was quick to setup, and painless to use.  First define your object to object mappings:

1: Mapper.CreateMap<RADE.BO.Domain.Application, RADE.BO.Entity.Application>();

 

Next, whip up a small conversion function:

1: /// <summary> 2: /// Convert a DTOApplication to abnd 3: /// </summary> 4: /// <param name="app"></param> 5: /// <returns></returns> 6: public static RADE.BO.Domain.Application Convert(Application app) 7: { 8: if (app == null) return null; 9: LoadDtoMappings(); 10: return AutoMapper.Mapper.Map<Application, RADE.BO.Domain.Application>(app); 11: } 12:  13: /// <summary> 14: /// Convert a list of domain Application objects to DTO Application objects 15: /// </summary> 16: /// <param name="apps">List of domain Application objects</param> 17: /// <returns>List of DTO Application objects</returns> 18: public static List<Application> Convert(List<RADE.BO.Domain.Application> apps) 19: { 20: LoadDtoMappings(); 21: return apps.Select(AutoMapper.Mapper.Map<RADE.BO.Domain.Application, Application>).ToList(); 22: }

Page 1 of 2 | Next page