/// <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); }
/// <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 InstanceInterceptionPolicy(interceptor), typeToIntercept); return(this); }
/// <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); }
/// <summary> /// API to configure interception for a type. /// </summary> /// <param name="typeToIntercept">Type to intercept.</param> /// <param name="name">Name type is registered under.</param> /// <param name="interceptor">Instance interceptor to use.</param> /// <returns>This extension object.</returns> public Interception SetInterceptorFor(Type typeToIntercept, string name, IInstanceInterceptor interceptor) { Guard.ArgumentNotNull(typeToIntercept, "typeToIntercept"); Guard.ArgumentNotNull(interceptor, "interceptor"); GuardTypeInterceptable(typeToIntercept, interceptor); NamedTypeBuildKey key = new NamedTypeBuildKey(typeToIntercept, name); InstanceInterceptionPolicy policy = new InstanceInterceptionPolicy(interceptor); Context.Policies.Set <IInstanceInterceptionPolicy>(policy, key); return(this); }
public override void AddPolicies(Type typeToCreate, string name, IPolicyList policies) { Guard.ArgumentNotNull(typeToCreate, "typeToCreate"); PropertyInfo propInfo = typeToCreate.GetProperty(propertyName); GuardPropertyExists(propInfo, typeToCreate, propertyName); GuardPropertyIsSettable(propInfo); GuardPropertyIsNotIndexer(propInfo); InitializeParameterValue(propInfo); GuardPropertyValueIsCompatible(propInfo, parameterValue); SpecifiedPropertiesSelectorPolicy selector = GetSelectorPolicy(policies, typeToCreate, name); selector.AddPropertyAndValue(propInfo, parameterValue); }
/// <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); }
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(GetStrategies().Reverse(), lifetimeContainer, policies, null, o); context.Strategies.ExecuteTearDown(context); } catch (Exception ex) { throw new ResolutionFailedException(o.GetType(), null, ex, context); } }
/// <summary> /// Create an instance of <see cref="InjectionParameter"/> that stores /// the given value, using the runtime type of that value as the /// type of the parameter. /// </summary> /// <param name="parameterValue">Value to be injected for this parameter.</param> public InjectionParameter(object parameterValue) : base(parameterValue.GetType()) { Guard.ArgumentNotNull(parameterValue, "parameterValue"); this.parameterValue = parameterValue; }
// 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)); }
// FxCop warning suppressed: false positive, Guard class is doing validation 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)); }