/// <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);
        }
Esempio n. 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);
        }
Esempio n. 3
0
        /// <summary>
        /// RegisterType a type mapping with the container, where the created instances will use
        /// the given <see cref="LifetimeManager"/>.
        /// </summary>
        /// <param name="from"><see cref="Type"/> that will be requested.</param>
        /// <param name="to"><see cref="Type"/> that will actually be returned.</param>
        /// <param name="name">Name to use for registration, null if a default registration.</param>
        /// <param name="lifetimeManager">The <see cref="LifetimeManager"/> that controls the lifetime
        /// of the returned instance.</param>
        /// <param name="injectionMembers">Injection configuration objects.</param>
        /// <returns>The <see cref="UnityContainer"/> object that this method was called on (this in C#, Me in Visual Basic).</returns>
        public IUnityContainer RegisterType(Type from, Type to, string name, LifetimeManager lifetimeManager, InjectionMember[] injectionMembers)
        {
            Guard.ArgumentNotNull(to, "to");
            Guard.ArgumentNotNull(injectionMembers, "injectionMembers");

            if (string.IsNullOrEmpty(name))
            {
                name = null;
            }

            if (from != null && !from.GetTypeInfo().IsGenericType && !to.GetTypeInfo().IsGenericType)
            {
                Guard.TypeIsAssignable(from, to, "from");
            }

            Registering(this, new RegisterEventArgs(from, to, name, lifetimeManager));

            if (injectionMembers.Length > 0)
            {
                ClearExistingBuildPlan(to, name);
                foreach (var member in injectionMembers)
                {
                    member.AddPolicies(from, to, name, policies);
                }
            }
            return this;
        }