We're using constructor-based dependency injection, AutoMapper and Unity on a codebase.
We have wrapped AutoMapper with a generic interface...
public interface IMapper<TSource, TDestination>
{
TDestination Map(TSource source);
}
And a class that implements this interface...
public class AutomaticMapper<TSource, TDestination> : IMapper<TSource, TDestination>
{
public TDestination Map(TSource source)
{
return AutoMapper.Mapper.Map<TSource, TDestination>(source);
}
}
This works well, but it means that for every mapping we define in the AutoMapper configuration we need to perform an additional UnityContainer.RegisterType
.
These type mappings are almost always of the form
container.RegisterType<IMapper<ClassA, ClassB>, AutomaticMapper<ClassA, ClassB>>();
Is there any way that we can tell unity to use a default type mapping that maps from IMapper
to AutomaticMapper
using the same TSource
and TDestination
for each of them?