private static void RegisterNamedMappings(IEnumerable <Type> source, AutomapperConfig configuration) { var namedMappings = source.Select(t => Tuple.Create(t, t.GetMapAsName())) .Where(pair => pair.Item2 != null); foreach (var namedMapping in namedMappings) { configuration.AndUseNamedMappingFor(namedMapping.Item1, namedMapping.Item2); } }
private void DoMerge(AutomapperConfig config) { Contract.Requires(config.doNotMapTypes != null); Contract.Requires(config.explicitNamedMappings != null); Contract.Requires(config.multimapTypes != null); Contract.Requires(config.policyInjectionTypes != null); Contract.Requires(config.customLifetimeManagerTypes != null); doNotMapTypes.AddRange(config.doNotMapTypes); explicitNamedMappings.AddRange(config.explicitNamedMappings); multimapTypes.AddRange(config.multimapTypes); policyInjectionTypes.AddRange(config.policyInjectionTypes); customLifetimeManagerTypes.AddRange(config.customLifetimeManagerTypes); }
private static void RegisterLifetimeManagers(IEnumerable <Type> source, AutomapperConfig configuration) { var mapMethodCallsite = typeof(AutomapperConfig).GetMethod("AndMapWithLifetimeManager").GetGenericMethodDefinition(); foreach (var lifetimeManagerGrouping in source.Select(s => new { Source = s, LifetimeManagers = s.GetCustomAttributes(typeof(CustomLifetimeManagerAttribute), false).Cast <CustomLifetimeManagerAttribute>() }) .SelectMany(s => s.LifetimeManagers.Select(l => new { Source = s.Source, LifetimeManager = l })) .GroupBy(s => s.LifetimeManager)) { if (!typeof(LifetimeManager).IsAssignableFrom(lifetimeManagerGrouping.Key.LifetimeManagerType)) { throw new InvalidOperationException(String.Format("The type {0} has been marked with the type {1} as a Lifetime Manager; lifetime managers must derive from LifetimeManager.", lifetimeManagerGrouping.FirstOrDefault().Source.Name, lifetimeManagerGrouping.Key.LifetimeManagerType.FullName)); } try { mapMethodCallsite.MakeGenericMethod(lifetimeManagerGrouping.Key.LifetimeManagerType) .Invoke(configuration, new[] { lifetimeManagerGrouping.Select(x => x.Source).ToArray() }); } catch (Exception ex) { throw ex.InnerException; } } }
internal AutomapperConfig MergeWith(AutomapperConfig sourceConfig) { DoMerge(sourceConfig); return(this); }