Here is how I solved it:
I defined an IMappingCreator
interface:
public interface IMappingCreator
{
void CreateMappings();
}
I went ahead and implemented a class with that interface (I'm using MEF as DI container, that's where the attributes are comming from) which is put into the DI container as IMappingCreator
:
[Export(typeof(IMappingCreator))]
public class Mapping : IMappingCreator
{
private readonly IRefTypesLookup iRefTypesLookup;
[ImportingConstructor]
public Mapping(IRefTypesLookup rtl)
{
iRefTypesLookup = rtl;
}
public void CreateMappings()
{
Mapper.CreateMap<Journal, DisplayJournal>().AfterMap((j, dj) => dj.RefTypeName = iRefTypesLookup.Lookup(j.RefTypeID));
}
}
Finally, in my application startup, I fetch all instances of that interface in the container and call the CreateMappings
method on them:
var mappings = container.GetExportedValues<IMappingCreator>();
foreach (IMappingCreator mc in mappings)
{
mc.CreateMappings();
}
This makes the initial setup quite easy, as all the creation happens in one place, and you can have as many mapping creators as you want (however, you should keep those to a minimum, maybe once per project or so, grabbing all needed services for mapping the specific types in that project).