/// <inheritdoc />
        IUnityContainer IUnityContainerAsync.RegisterFactory(IEnumerable <Type> interfaces, string name, Func <IUnityContainer, Type, string, object> factory, IFactoryLifetimeManager lifetimeManager)
        {
            // Validate input
            // TODO: Move to diagnostic

            if (null == interfaces)
            {
                throw new ArgumentNullException(nameof(interfaces));
            }
            if (null == factory)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            if (null == lifetimeManager)
            {
                lifetimeManager = TransientLifetimeManager.Instance;
            }
            if (((LifetimeManager)lifetimeManager).InUse)
            {
                throw new InvalidOperationException(LifetimeManagerInUse);
            }

            // Create registration and add to appropriate storage
            var container = lifetimeManager is SingletonLifetimeManager ? _root : this;

            // TODO: InjectionFactory
            #pragma warning disable CS0618
            var injectionFactory = new InjectionFactory(factory);
            #pragma warning restore CS0618

            var injectionMembers = new InjectionMember[] { injectionFactory };
            var registration     = new ExplicitRegistration(_validators, (LifetimeManager)lifetimeManager, injectionMembers);

            // Add Injection Members
            //injectionFactory.AddPolicies<BuilderContext, ContainerRegistration>(
            //    type, type, name, ref registration);

            // Register interfaces
            var replaced = container.AddOrReplaceRegistrations(interfaces, name, registration)
                           .ToArray();

            // Release replaced registrations
            if (0 != replaced.Length)
            {
                Task.Factory.StartNew(() =>
                {
                    foreach (ImplicitRegistration previous in replaced)
                    {
                        if (0 == previous.Release() && previous.LifetimeManager is IDisposable disposable)
                        {
                            // Dispose replaced lifetime manager
                            container.LifetimeContainer.Remove(disposable);
                            disposable.Dispose();
                        }
                    }
                });
            }

            return(this);
        }
Exemple #2
0
        /// <inheritdoc />
        public IUnityContainer RegisterFactory(Type type, string name, Func <IUnityContainer, Type, string, object> factory, IFactoryLifetimeManager lifetimeManager)
        {
            // Validate input
            if (null == type)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (null == factory)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            if (null == lifetimeManager)
            {
                lifetimeManager = TransientLifetimeManager.Instance;
            }
            if (((LifetimeManager)lifetimeManager).InUse)
            {
                throw new InvalidOperationException(LifetimeManagerInUse);
            }

            // Create registration and add to appropriate storage
            var container = lifetimeManager is SingletonLifetimeManager ? _root : this;

#pragma warning disable CS0618 // TODO: InjectionFactory
            var injectionFactory = new InjectionFactory(factory);
#pragma warning restore CS0618
            var injectionMembers = new InjectionMember[] { injectionFactory };
            var registration     = new ContainerRegistration(_validators, type, ((LifetimeManager)lifetimeManager), injectionMembers);

            // Add or replace existing
            var previous = container.Register(type, name, registration);
            if (previous is ContainerRegistration old &&
                old.LifetimeManager is IDisposable disposable)
            {
                // Dispose replaced lifetime manager
                container.LifetimeContainer.Remove(disposable);
                disposable.Dispose();
            }

            // If Disposable add to container's lifetime
            if (lifetimeManager is IDisposable manager)
            {
                container.LifetimeContainer.Add(manager);
            }

            // Add Injection Members
            injectionFactory.AddPolicies <BuilderContext, ContainerRegistration>(
                type, type, name, ref registration);

            // Check what strategies to run
            registration.BuildChain = _strategiesChain.ToArray()
                                      .Where(strategy => strategy.RequiredToBuildType(this,
                                                                                      type, registration, injectionMembers))
                                      .ToArray();
            // Raise event
            container.Registering?.Invoke(this, new RegisterEventArgs(type, type, name,
                                                                      ((LifetimeManager)lifetimeManager)));
            return(this);
        }