/// <summary> /// Merge multiple DI Factories into a single factory. If settings or object mappings collide, it will throw /// an InvalidOperation exception. /// </summary> /// <param name="originalContainer"></param> /// <param name="extraContainers"></param> /// <returns></returns> public static DIFactory Merge(DIFactory originalContainer, params DIFactory[] extraContainers) { DIFactory newFactory = new DIFactory(originalContainer); foreach (var container in extraContainers) { newFactory.MergeWithSelf(container); } return(newFactory); }
/// <summary> /// Adopt the given new factory's create/release routines and settings and merge it with our own. /// Fails with InvalidOperationException if there are duplicate definitions. /// </summary> /// <param name="newFactory"></param> public void MergeWithSelf(DIFactory newFactory) { //For each container, merge the creation mappings foreach (var cMapping in newFactory.CreationMapping) { if (CreationMapping.ContainsKey(cMapping.Key)) { throw new InvalidOperationException("Duplicate methods for creating type " + cMapping.Key.ToString()); } else { CreationMapping.Add(cMapping.Key, cMapping.Value); } } //For each container, merge the release mappings foreach (var rMapping in newFactory.ReleaseMapping) { if (ReleaseMapping.ContainsKey(rMapping.Key)) { throw new InvalidOperationException("Duplicate methods for releasing type " + rMapping.Key.ToString()); } else { ReleaseMapping.Add(rMapping.Key, rMapping.Value); } } //Finally, for each container, merge the settings. foreach (var setting in newFactory.Settings) { if (Settings.ContainsKey(setting.Key)) { throw new InvalidOperationException("Duplicate settings key: " + setting.Key); } else { Settings.Add(setting.Key, setting.Value); } } }
public DIFactory(DIFactory copy, bool setupLogger = true) : this(setupLogger) { CreationMapping = new Dictionary <Type, Func <DIFactory, object> >(copy.CreationMapping); ReleaseMapping = new Dictionary <Type, Action <DIFactory, object> >(copy.ReleaseMapping); Settings = new Dictionary <string, object>(copy.Settings); }