예제 #1
0
        /// <summary>
        /// Create a child container.
        /// </summary>
        /// <remarks>
        /// A child container shares the parent's configuration, but can be configured with different
        /// settings or lifetime.</remarks>
        /// <returns>The new child container.</returns>
        public IUnityContainer CreateChildContainer()
        {
            var child = new UnityContainer(this);

            ChildContainerCreated?.Invoke(this, new ChildContainerCreatedEventArgs(child._context));
            return(child);
        }
        /// <inheritdoc />
        object IUnityContainer.BuildUp(Type type, object existing, string name, params ResolverOverride[] overrides)
        {
            // Verify arguments
            if (null == type)
            {
                throw new ArgumentNullException(nameof(type));
            }

            // Validate if they are assignable
            if (null != existing && null != TypeValidator)
            {
                TypeValidator(type, existing.GetType());
            }

            var registration = (InternalRegistration)GetRegistration(type, name);
            var container    = registration.Get(typeof(LifetimeManager)) is ContainerControlledLifetimeManager manager
                          ? (UnityContainer)manager.Scope
                          : this;

            var context = new BuilderContext
            {
                List             = new PolicyList(),
                Lifetime         = container.LifetimeContainer,
                Existing         = existing,
                Overrides        = null != overrides && 0 == overrides.Length ? null : overrides,
                Registration     = registration,
                RegistrationType = type,
                Name             = name,
                ExecutePlan      = ContextExecutePlan,
                ResolvePlan      = ContextResolvePlan,
                Type             = registration is ContainerRegistration containerRegistration
                                     ? containerRegistration.Type : type
            };

            return(ExecutePlan(ref context));
        }

        #endregion


        #region Child container management

        /// <inheritdoc />
        IUnityContainer IUnityContainer.CreateChildContainer()
        {
            var child = new UnityContainer(this);

            ChildContainerCreated?.Invoke(this, new ChildContainerCreatedEventArgs(child._context));
            return(child);
        }

        /// <inheritdoc />
        IUnityContainer IUnityContainer.Parent => _parent;

        #endregion
    }