/// <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); }
/// <summary> /// Return instances of all registered types requested. /// </summary> /// <remarks> /// <para> /// This method is useful if you've registered multiple types with the same /// <see cref="Type"/> but different names. /// </para> /// <para> /// Be aware that this method does NOT return an instance for the default (unnamed) registration. /// </para> /// </remarks> /// <param name="t">The type requested.</param> /// <param name="resolverOverrides">Any overrides for the resolve calls.</param> /// <returns>Set of objects of type <paramref name="t"/>.</returns> public IEnumerable <object> ResolveAll(Type t, params ResolverOverride[] resolverOverrides) { Guard.ArgumentNotNull(t, "t"); var result = this.Resolve(t.MakeArrayType(), resolverOverrides); return(result is IEnumerable <object> ?(IEnumerable <object>)result : ((Array)result).Cast <object>()); }
/// <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 responsible /// 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 IUnityContainer RegisterInstance(Type t, string name, object instance, LifetimeManager lifetime) { Guard.ArgumentNotNull(instance, "instance"); Guard.ArgumentNotNull(lifetime, "lifetime"); Guard.InstanceIsAssignable(t, instance, "instance"); RegisteringInstance(this, new RegisterInstanceEventArgs(t, instance, name, lifetime)); return(this); }
/// <summary> /// API to configure the default interception settings for a type. /// </summary> /// <param name="typeToIntercept">Type the interception is being configured for.</param> /// <param name="interceptor">The interceptor to use by default.</param> /// <returns>This extension object.</returns> public Interception SetDefaultInterceptorFor(Type typeToIntercept, IInstanceInterceptor interceptor) { Guard.ArgumentNotNull(typeToIntercept, "typeToIntercept"); Guard.ArgumentNotNull(interceptor, "interceptor"); GuardTypeInterceptable(typeToIntercept, interceptor); Context.Policies.Set <IInstanceInterceptionPolicy>(new FixedInstanceInterceptionPolicy(interceptor), typeToIntercept); // add policy injection behavior if using this configuration API to set the interceptor var interceptionBehaviorsPolicy = new InterceptionBehaviorsPolicy(); interceptionBehaviorsPolicy.AddBehaviorKey(NamedTypeBuildKey.Make <PolicyInjectionBehavior>()); Context.Policies.Set <IInterceptionBehaviorsPolicy>(interceptionBehaviorsPolicy, typeToIntercept); return(this); }
/// <summary> /// Do the PreBuildUp stage of construction. This is where the actual work is performed. /// </summary> /// <param name="context">Current build context.</param> public override void PreBuildUp(IBuilderContext context) { Guard.ArgumentNotNull(context, "context"); Type typeToBuild = context.BuildKey.Type; if (typeToBuild.IsArray && typeToBuild.GetArrayRank() == 1) { Type elementType = typeToBuild.GetElementType(); MethodInfo resolverMethod = GenericResolveArrayMethod.MakeGenericMethod(elementType); ArrayResolver resolver = (ArrayResolver)resolverMethod.CreateDelegate(typeof(ArrayResolver)); context.Existing = resolver(context); context.BuildComplete = true; } }
/// <summary> /// Run an existing object through the container, and clean it up. /// </summary> /// <param name="o">The object to tear down.</param> public void Teardown(object o) { IBuilderContext context = null; try { Guard.ArgumentNotNull(o, "o"); context = new BuilderContext(this, GetStrategies().Reverse(), lifetimeContainer, policies, null, o); context.Strategies.ExecuteTearDown(context); } catch (Exception ex) { throw new ResolutionFailedException(o.GetType(), null, ex, context); } }
/// <summary> /// Starts the definition of a new <see cref="RuleDrivenPolicy"/>. /// </summary> /// <param name="policyName">The policy name.</param> /// <returns></returns> /// <remarks>This is a convenient way for defining a new policy and the <see cref="IMatchingRule"/> /// instances and <see cref="ICallHandler"/> instances that are required by a policy. /// <para/> /// This mechanism is just a shortcut for what can be natively expressed by wiring up together objects /// with repeated calls to the <see cref="IUnityContainer.RegisterType"/> method. /// </remarks> public PolicyDefinition AddPolicy(string policyName) { Guard.ArgumentNotNullOrEmpty(policyName, "policyName"); return(new PolicyDefinition(policyName, this)); }
/// <summary> /// Run an existing object through the container and perform injection on it. /// </summary> /// <remarks> /// <para> /// This method is useful when you don't control the construction of an /// instance (ASP.NET pages or objects created via XAML, for instance) /// but you still want properties and other injection performed. /// </para></remarks> /// <param name="t"><see cref="Type"/> of object to perform injection on.</param> /// <param name="existing">Instance to build up.</param> /// <param name="name">name to use when looking up the typemappings and other configurations.</param> /// <param name="resolverOverrides">Any overrides for the buildup.</param> /// <returns>The resulting object. By default, this will be <paramref name="existing"/>, but /// container extensions may add things like automatic proxy creation which would /// cause this to return a different object (but still type compatible with <paramref name="t"/>).</returns> public object BuildUp(Type t, object existing, string name, params ResolverOverride[] resolverOverrides) { Guard.ArgumentNotNull(existing, "existing"); Guard.InstanceIsAssignable(t, existing, "existing"); return(DoBuildUp(t, existing, name, resolverOverrides)); }
/// <summary> /// Return instances of all registered types requested. /// </summary> /// <remarks> /// <para> /// This method is useful if you've registered multiple types with the same /// <see cref="Type"/> but different names. /// </para> /// <para> /// Be aware that this method does NOT return an instance for the default (unnamed) registration. /// </para> /// </remarks> /// <param name="t">The type requested.</param> /// <param name="resolverOverrides">Any overrides for the resolve calls.</param> /// <returns>Set of objects of type <paramref name="t"/>.</returns> public IEnumerable <object> ResolveAll(Type t, params ResolverOverride[] resolverOverrides) { Guard.ArgumentNotNull(t, "t"); return((IEnumerable <object>) this.Resolve(t.MakeArrayType(), resolverOverrides)); }