/// <summary> /// Create a <see cref="Unity.UnityContainer"/> with the given parent container. /// </summary> /// <param name="parent">The parent <see cref="Unity.UnityContainer"/>. The current object /// will apply its own settings first, and then check the parent for additional ones.</param> private UnityContainer(UnityContainer parent) { // WithLifetime LifetimeContainer = new LifetimeContainer(this); // Context and policies _context = new ContainerContext(this); // Parent _parent = parent ?? throw new ArgumentNullException(nameof(parent)); _parent.LifetimeContainer.Add(this); _root = _parent._root; SetDefaultPolicies = parent.SetDefaultPolicies; // Methods _get = _parent._get; _getGenericRegistration = _parent._getGenericRegistration; _isExplicitlyRegistered = _parent._isExplicitlyRegistered; IsTypeExplicitlyRegistered = _parent.IsTypeExplicitlyRegistered; GetRegistration = _parent.GetRegistration; Register = CreateAndSetOrUpdate; GetPolicy = parent.GetPolicy; SetPolicy = CreateAndSetPolicy; ClearPolicy = delegate { }; // Strategies _strategies = _parent._strategies; _strategiesChain = _parent._strategiesChain; _strategies.Invalidated += OnStrategiesChanged; // Caches SetDefaultPolicies(this); }
protected virtual void Dispose(bool disposing) { if (!disposing) { return; } List <Exception> exceptions = null; try { GetPolicy = (type, name, policyInterface) => throw new ObjectDisposedException($"{DebugName()}"); _strategies.Invalidated -= OnStrategiesChanged; _parent?.LifetimeContainer.Remove(this); LifetimeContainer.Dispose(); } catch (Exception exception) { exceptions = new List <Exception> { exception }; } if (null != _extensions) { foreach (IDisposable disposable in _extensions.OfType <IDisposable>() .ToList()) { try { disposable.Dispose(); } catch (Exception e) { if (null == exceptions) { exceptions = new List <Exception>(); } exceptions.Add(e); } } _extensions = null; } lock (GetRegistration) { _registrations = new Registrations(1); } if (null != exceptions && exceptions.Count == 1) { throw exceptions[0]; } else if (null != exceptions && exceptions.Count > 1) { throw new AggregateException(exceptions); } }
private void SetupChildContainerBehaviors() { lock (_syncRoot) { if (null == _registrations) { _registrations = new Registrations(ContainerInitialCapacity); Set(null, null, Defaults); Register = AddOrUpdate; GetPolicy = Get; SetPolicy = Set; ClearPolicy = Clear; GetRegistration = GetDynamicRegistration; _get = (type, name) => Get(type, name) ?? _parent._get(type, name); _getGenericRegistration = GetOrAddGeneric; IsTypeExplicitlyRegistered = IsTypeTypeExplicitlyRegisteredLocally; _isExplicitlyRegistered = IsExplicitlyRegisteredLocally; } } }