In my infrastructure layer of my projects I regularly include the following interface:

public interface IMapper<Input, Output> { Output MapFrom(Input input); }

This interface should be fairly self-explanatory, it takes in an item of type Input and spits out an item of type Output. For those who care about details (and I do) it is also good to note that this interface matches the signature for a Converter generic delegate.

One of the (many) places that I use implementations of this interface is in the service layer to marshal data back up to the higher level layers as DTOS. Here is an example of a simple Mapper:

public class DepartmentDisplayItemDTOMapper : IDepartmentDisplayItemDTOMapper { public DepartmentDisplayItemDO MapFrom(IDepartment department) { return new DepartmentDisplayItemDO
(department.Name, department.Id); } }

One of the ways that I would have leveraged this in the past would have been to inject the service layer component with the appropriate mapper and then map items that were returned from the repository in place.

Here is another alternative that takes advantage of a type I am calling a MappingEnumerable<T>. Take a look at the code for the class:

public class MappingEnumerable<Input, Output> : IMappingEnumerable<Input, Output> { private IMapper<Input, Output> mapper; private IEnumerable<Input> inputItems; public MappingEnumerable(IMapper<Input, Output> mapper, IEnumerable<Input> inputItems) { this.mapper = mapper; this.inputItems = inputItems; } IEnumerator<Output> IEnumerable<Output>.GetEnumerator() { return MapAllFrom(inputItems).GetEnumerator(); } public IEnumerator GetEnumerator() { return ((IEnumerable<Output>) this).GetEnumerator(); } private IEnumerable<Output> MapAllFrom(IEnumerable<Input> input) { foreach (Input item in input) { yield return mapper.MapFrom(item); } } }

Using this class from the service layer is as simple as:


public IEnumerable<DepartmentDisplayItemDO> GetAllSubdepartmentsOf(long departmentId) { IDepartment parentDepartment = departmentRepository.FindBy(departmentId); IEnumerable<IDepartment> subdepartments = departmentRepository.AllSubDepartmentsOf(parentDepartment); return new MappingEnumerable<IDepartment, DepartmentDisplayItemDO> (subdepartments, departmentDisplayItemDTOMapper); }

Notice that the MappingEnumerable decorates the existing enumerable with mapping behaviour. Because the MappingEnumerable is itself an implementation of an IEnumerable<T> interface, the mapping of each item will not start until the iteration of the enumerable happens.

Taking this class and applying it in the 3.5 world (I prefer using components vs free floating extension methods) can result in the following:

IDepartment parentDepartment = departmentRepository.FindBy(departmentId); IEnumerable<IDepartment> subdepartments = departmentRepository.AllSubDepartmentsOf(parentDepartment); return subdepartments.MapAllUsing(departmentDisplayItemDTOMapper);

Where MapAllUsing is an extension method on IEnumerable which lives in my infrastructure layer. The simplified implementation of MapAllUsing just delegates the work to the MappingEnumerable class: 

public static IEnumerable<Output> MapAllUsing<T,Output>(this IEnumerable<T> itemsToMap,IMapper<T,Output> mapper) { return new MappingEnumerable<T, Output>
(mapper,itemsToMap); }

One more short example to show how I leverage this pattern in my apps take a look at this method on a DatabaseGateway interface:

IEnumerable<DataRow> GetASetOfRowsUsingA(IQuery query);

A simple method to return an enumerable of DataRows. You can now leverage this to do things like:

public IEnumerable<DomainObject> GetAllUsing(IQuery query) { return gateway.GetASetOfRowsUsingA(query).MapAllUsing(dbMapper); }

Where the mapper in this case knows how to hydrate datarows into domain objects of the specific (DomainObject) type the mapper is defined for.

One of the things I tell people in class (and work) is that IEnumerable is the ultimate Separated Interface and this post just scratches the surface of the things you can do by just getting creative with the IEnumerable interface.


Develop With Passion!!

This entry was posted in C#, Programming. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

One Response to MappingEnumerable<T>

  1. Udi Dahan says:

    Have you considered just mapping your DTO’s with your O/R Mapper? It seems to be what the O/RM was designed to do – simpler actually, no fancy inheritance or anything. In which case, your service layer just uses the DTO type as the for the specific Get and Query methods.

    I have a blog post covering this in a bit more detail here:

Leave a Reply