예제 #1
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");
            if (string.IsNullOrEmpty(name))
            {
                name = null;
            }

            if (from != null && !from.IsGenericType && !to.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);
        }
예제 #2
0
 /// <summary>
 /// RegisterType an instance with the container.
 /// </summary>
 /// <remarks>
 /// <para>
 /// Instance registration is much like setting a type as a singleton, except that instead
 /// of the container creating the instance the first time it is requested, the user
 /// creates the instance ahead of type and adds that instance to the container.
 /// </para>
 /// </remarks>
 /// <param name="t">Type of instance to register (may be an implemented interface instead of the full type).</param>
 /// <param name="instance">Object to returned.</param>
 /// <param name="name">Name for registration.</param>
 /// <param name="lifetime">
 /// <para>If true, the container will take over the lifetime of the instance,
 /// calling Dispose on it (if it's <see cref="IDisposable"/>) when the container is Disposed.</para>
 /// <para>
 ///  If false, container will not maintain a strong reference to <paramref name="instance"/>. User is reponsible
 /// for disposing instance, and for keeping the instance from being garbage collected.</para></param>
 /// <returns>The <see cref="UnityContainer"/> object that this method was called on (this in C#, Me in Visual Basic).</returns>
 public override IUnityContainer RegisterInstance(Type t, string name, object instance, LifetimeManager lifetime)
 {
     Guard.ArgumentNotNull(instance, "instance");
     Guard.ArgumentNotNull(lifetime, "lifetime");
     Guard.TypeIsAssignable(t, instance.GetType(), "instance");
     registeringInstance(this,
                         new RegisterInstanceEventArgs(t,
                                                       instance,
                                                       name,
                                                       lifetime));
     return(this);
 }
예제 #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 override IUnityContainer RegisterType(Type from, Type to, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
        {
            if (to != null && !from.IsGenericType && !to.IsGenericType)
            {
                Guard.TypeIsAssignable(from, to, "from");
            }
            registering(this, new RegisterEventArgs(from, to, name, lifetimeManager));

            if (injectionMembers.Length > 0)
            {
                Configure <InjectedMembers>()
                .ConfigureInjectionFor(to ?? from, name, injectionMembers);
            }
            return(this);
        }
예제 #4
0
 // FxCop warning suppressed: false positive, Guard class is doing validation
 public override object BuildUp(Type t, object existing, string name)
 {
     Guard.ArgumentNotNull(existing, "existing");
     Guard.TypeIsAssignable(t, existing.GetType(), "existing");
     return(DoBuildUp(t, existing, name));
 }