As far as I know, in Automapper there is no built-in support for user agreements on registering a cartographer, but you can dry it a little with some reflection:
var mappingDictionary = new Dictionary<Type, Type> { {typeof (Headline), typeof (HeadlineModel)}, {typeof (Event), typeof (EventModel)}, //... }; foreach (var sourceType in mappingDictionary.Keys) { Mapper.CreateMap( typeof (PagedResult<>).MakeGenericType(sourceType), typeof (PagedResult<>).MakeGenericType(mappingDictionary[sourceType])); }
Or, if you always follow your agreement Headline HeadlineModel , etc. With one more thought, you do not need to create a mapping manually:
var modelAssembly = Assembly.GetAssembly(typeof(HeadlineModel)); var otherAssembly = Assembly.GetAssembly(typeof(Headline)); foreach (var destinationType in modelAssembly.GetTypes() .Where(t => t.Namespace == "ModelNaspace" && t.Name.EndsWith("Model"))) { var destinationName = destinationType.Name.Replace("Model", ""); var sourceType = otherAssembly.GetTypes() .SingleOrDefault(t => t.Namespace == "OtherNamespace" && t.Name == destinationName); if (sourceType == null) {
source share