/// <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);
        }
Пример #2
0
        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;
                }
            }
        }