Home > .NET, WCF > DTO Assembler

DTO Assembler

September 1st, 2009 Garrett Leave a comment Go to comments

When writing services that pass data between processes, it is oftentimes beneficial and wise to package the data in simple classes called DataTransferObjects (DTOs).  The database-matching Entity objects are not good choices for serialization since they may contain too much information, too little information, could be many layers deep, and expose the database structure to consuming clients.

The Assembler pattern is used to build up the DTO objects before sending results back from a method and is also responsible for reversing this process when clients pass DTOs to the service.  This build process involves mapping Entity classes to DTO classes, but there will not necessarily be a one-to-one correspondence between properties.  In either case, the process of mapping matching properties can be a laborious programming task.

Enter the AutoMapper

One option for overcoming this chore is to use generated code, which can be sufficient for exact matches but doesn’t address more complicated scenarios.  The other option is to use mapping code, and AutoMapper (http://www.codeplex.com/AutoMapper) is a CodePlex project meant to solve exactly this problem.  By default, the AutoMapper library copies property values from one class to another based on property names and also allows for more complicated mappings.

DtoAssembler

For my particular set of DTOs, the mappings were mostly one-to-one to the underlying database entities and did not require many changes.  To simplify things, I created a generic DtoAssembler class that takes two class types – TSource and TDestination – as the input and output types for the mappings.  Next, we simply create a map using the CreateMap static method and then call the Map to perform the conversion.

using AutoMapper;

public static class DtoAssembler<TSource, TDestination>
{
        public static void MapObject(TSource entity, TDestination destination)
        {
            Mapper.CreateMap<TSource, TDestination>();
            Mapper.Map<TSource, TDestination>(entity, destination);
        }

        public static TDestination MapObject(TSource entity)
        {
            Mapper.CreateMap<TSource, TDestination>();

            TDestination dto = Mapper.Map<TSource, TDestination>(entity);

            return dto;
        }

        public static List<TDestination> MapList(List<TSource> entities)
        {
            List<TDestination> dtoList = new List<TDestination>();

            foreach (TSource entity in entities)
            {
                dtoList.Add(MapObject(entity));
            }

            return dtoList;
        }
}

From within the service code, the call is as simple as this to create the DTO:

TeamDto teamDto = DtoAssembler<TeamEntity, TeamDto>.MapObject(team);

As you can see, creating a simple DTO mapping can be greatly simplified by using the AutoMapper. For more complicated mapping scenarios, take a look at the AutoMapper documentation for examples.

Categories: .NET, WCF
  1. RH
    September 30th, 2009 at 13:22 | #1

    This is quite helpful information!

  1. No trackbacks yet.