AutoMapper is an excellent object-object mapper which uses a fluent configuration API and conventions to perform mapping with ease. Read more about AutoMapper here and my post here.
The Problem
When using AutoMapper you need to create a map between the objects you want to map. Of course, the maps must be created before any mapping is performed by AutoMapper. When I first started using AutoMapper I created a class (MappingConfigurer) to handle the creation of maps for AutoMapper. However, this class grew quite large over time when the system grew larger. After a while there were over 50 types getting mapped by AutoMapper. Having all of these initialized in the same class and method was a bit cumbersome after a while.
The Solution (well, one solution)
To remedy this situation I created an interface IMappingConfiguration. The interface only has one method Configure(), each type implementing this method
must create and configure the map its responsible for.
For example, the UserMapper is responsible for mapping from User to UserViewModel. The UserMapper implements the interface IMappingConfiguration. In the method Configure() it creates the map needed:
public void Configure() {
Mapper.CreateMap<User, UserViewModel>()
.ForMember(viewModel => viewModel.Phone,
opt => opt.MapFrom(user => user.Telephone.PhoneNumber)));
}
The UserMapper class also has a method Map to do the actual mapping.
public UserViewModel Map(User user) {
return Mapper.Map<User, UserViewModel>(user);
}
Now I have a class which is responsible for mapping User. The problem is that instead of having one giant method to create all of these maps I now have quite a lot of small classes.
To execute the Configure() method of all these mapping classes I use reflection to find all the types implementing the interface and executing the method. The first version was just plain type navigation using LINQ and reflection. But after a while I realized I needed this kind of functionality in other projects as well and in other scenarios. What I really wanted was an easy way to find all types implementing an interface and executing a method on the interface. Different ways of doing it. I could have used Castle Windsor for resolving all types implementing the IMappingConfiguration interface but I’m not using Castle Windsor in all of my projects and I don’t want to integrate Windsor just for this reason.
I started by writing down the way I wanted to be expressed:
FindTypes
.InAssembly(typeof(UserMapper).Assembly)
.Implementing<IMappingConfiguration>()
.Execute(mappingConfigurer => mappingConfigurer.Configure());
Higher level of abstraction and could potentially be used in other scenarios. For example, instead of executing a method it could return a list of types implementing the interface. Could even extend it to use Windsor. Instead of InAssembly, it could be InContainer(container), and it would use Windsor to resolve all types implementing the interface and still executing the method.
Code
This was the code I came up with (while watching an episode of Red Dwarf), so its at best a proof-of-concept 
The first part, which all it does it indicates where to look for types.
FindTypes.InAssembly(typeof(UserMapper).Assembly)
The code is quite simple, static method which returns an AssemblyNavigator with the supplied assembly as a parameter to the constructor.
public class FindTypes
{
public static AssemblyNavigator InAssembly(Assembly assembly)
{
return new AssemblyNavigator(assembly);
}
}
The next part of the expression, is just filtering out the types implementing the interface.
.Implementing<IMappingConfiguration>()
The code uses LINQ to select the types in the assembly that is implementing the interface. The most difficult part of this code was figuring out the way to invoke IsAssignableFrom(), very easy to do it the other way around.
public class AssemblyNavigator
{
private readonly Assembly _assembly;
public AssemblyNavigator(Assembly assembly)
{
_assembly = assembly;
}
public TypeList<T> Implementing<T>() where T : class
{
var listOfTypesImplementingInterface = from type in _assembly.GetTypes()
where typeof(T).IsAssignableFrom(type) && type.IsClass
select type;
return new TypeList<T>(listOfTypesImplementingInterface);
}
}
Also, note the type.IsClass in the where-clause. You’ll need this or otherwise you might end up with the interface it self. This method returns a TypeList<T> which is used in the last part of the expression.
.Execute(mapping => mapping.Configure());
The Execute() method creates an instance of the type and invokes the method supplied as an Action<T> on the instance.
public class TypeList<T> where T : class
{
private readonly IEnumerable<Type> _listOfTypes;
public TypeList(IEnumerable<Type> listOfTypes)
{
_listOfTypes = listOfTypes;
}
public void Execute(Action<T> action)
{
foreach (var type in _listOfTypes)
{
var instanceOfType = Activator.CreateInstance(type) as T;
action(instanceOfType);
}
}
}
That’s all the code that was needed to get it working. If it’s usable, maybe, it hides the details of the implementation.
At least it was fun writing it.
fe7b69a7-09c8-46d2-9320-15b6233907e4|3|4.3