private static IBuilderPolicy GetExtended(IPolicyList list, Type policyInterface, INamedType buildKey, Type buildType, out IPolicyList containingPolicyList) { containingPolicyList = null; if (null == buildType) { return(null); } // Check if generic if (buildType.GetTypeInfo().IsGenericType) { var newType = buildType.GetGenericTypeDefinition(); return(list.Get(newType, buildKey.Name, policyInterface, out containingPolicyList) ?? list.Get(newType, string.Empty, policyInterface, out containingPolicyList)); } // Check if array if (buildType.IsArray && buildType.GetArrayRank() == 1) { return(list.Get(typeof(Array), buildKey.Name, policyInterface, out containingPolicyList) ?? list.Get(typeof(Array), string.Empty, policyInterface, out containingPolicyList)); } // Check default for type return(list.Get(buildType, string.Empty, policyInterface, out containingPolicyList)); }
public static TPolicyInterface GetPolicy <TPolicyInterface>(this IPolicyList list, INamedType buildKey, out IPolicyList containingPolicyList) { return((TPolicyInterface)(list.GetPolicyForKey(typeof(TPolicyInterface), buildKey, out containingPolicyList) ?? (buildKey.Type.GetTypeInfo().IsGenericType ? list.Get(buildKey.Type.GetGenericTypeDefinition(), buildKey.Name, typeof(TPolicyInterface), out containingPolicyList) ?? list.Get(null, null, typeof(TPolicyInterface), out containingPolicyList) : list.Get(null, null, typeof(TPolicyInterface), out containingPolicyList)))); }
private ILifetimePolicy GetLifetimePolicy(IBuilderContext context, out IPolicyList source) { var policy = context.Policies.Get(context.OriginalBuildKey.Type, context.OriginalBuildKey.Name, typeof(ILifetimePolicy), out source); if (policy == null && context.OriginalBuildKey.Type.GetTypeInfo().IsGenericType) { policy = context.Policies.Get(context.BuildKey.Type.GetGenericTypeDefinition(), context.BuildKey.Name, typeof(ILifetimePolicy), out source); if (!(policy is ILifetimeFactoryPolicy factoryPolicy)) { return(null); } lock (_genericLifetimeManagerLock) { // check whether the policy for closed-generic has been added since first checked var newLifetime = (ILifetimePolicy)source.Get(context.OriginalBuildKey.Type, context.OriginalBuildKey.Name, typeof(ILifetimePolicy), out _); if (null == newLifetime) { newLifetime = factoryPolicy.CreateLifetimePolicy(); source.Set(context.OriginalBuildKey.Type, context.OriginalBuildKey.Name, typeof(ILifetimePolicy), newLifetime); if (newLifetime is IDisposable) { context.Lifetime.Add(newLifetime); } } return(newLifetime); } } return((ILifetimePolicy)policy); }
public void InjectionConstructorInsertsChooserForConstructorWithParameters() { string expectedString = "Hello"; int expectedInt = 12; var ctor = new InjectionConstructor(expectedString, expectedInt); var context = new MockBuilderContext { BuildKey = new NamedTypeBuildKey(typeof(GuineaPig)) }; IPolicyList policies = context.PersistentPolicies; ctor.AddPolicies(typeof(GuineaPig), policies); var selector = policies.Get <IConstructorSelectorPolicy>( new NamedTypeBuildKey(typeof(GuineaPig))); SelectedConstructor selected = selector.SelectConstructor(context, policies); string[] keys = selected.GetParameterKeys(); Assert.AreEqual(typeof(GuineaPig).GetConstructor(Sequence.Collect(typeof(string), typeof(int))), selected.Constructor); Assert.AreEqual(2, keys.Length); Assert.AreEqual(expectedString, (string)ResolveValue(policies, keys[0])); Assert.AreEqual(expectedInt, (int)ResolveValue(policies, keys[1])); }
public void InjectionConstructorSetsResolverForInterfaceToLookupInContainer() { InjectionConstructor ctor = new InjectionConstructor("Logger", typeof(ILogger)); TestingBuilderContext context = new TestingBuilderContext(); context.BuildKey = typeof(GuineaPig); IPolicyList policies = context.PersistentPolicies; ctor.AddPolicies(typeof(GuineaPig), policies); IConstructorSelectorPolicy selector = policies.Get <IConstructorSelectorPolicy>( new NamedTypeBuildKey(typeof(GuineaPig))); SelectedConstructor selected = selector.SelectConstructor(context); string[] keys = selected.GetParameterKeys(); Assert.AreEqual(typeof(GuineaPig).GetConstructor(Sequence.Collect(typeof(string), typeof(ILogger))), selected.Constructor); Assert.AreEqual(2, keys.Length); IDependencyResolverPolicy policy = context.Policies.Get <IDependencyResolverPolicy>(keys[1]); Assert.IsTrue(policy is NamedTypeDependencyResolverPolicy); }
public static TPolicyInterface Get <TPolicyInterface>(this IPolicyList policies, object buildKey, out IPolicyList containingPolicyList) where TPolicyInterface : IBuilderPolicy { Guard.ArgumentNotNull(policies, "policies"); return((TPolicyInterface)policies.Get(typeof(TPolicyInterface), buildKey, false, out containingPolicyList)); }
/// <summary> /// Gets an individual policy. /// </summary> /// <param name="policies"><see cref="IPolicyList"/> to search.</param> /// <param name="policyInterface">The interface the policy is registered under.</param> /// <param name="buildKey">The key the policy applies.</param> /// <param name="localOnly">true if the policy searches local only; otherwise false to seach up the parent chain.</param> /// <returns>The policy in the list, if present; returns null otherwise.</returns> public static IBuilderPolicy Get(this IPolicyList policies, Type policyInterface, object buildKey, bool localOnly) { IPolicyList containingPolicyList; return(policies.Get(policyInterface, buildKey, localOnly, out containingPolicyList)); }
/// <summary> /// Remove the resolvers for the given build key. /// </summary> /// <param name="policies">Policy list containing the build key.</param> /// <param name="buildKey">Build key.</param> public static void RemoveResolvers(IPolicyList policies, object buildKey) { IDependencyResolverTrackerPolicy tracker = policies.Get<IDependencyResolverTrackerPolicy>(buildKey); if (tracker != null) { tracker.RemoveResolvers(policies); } }
private Type GetMappedType(IPolicyList policies) { var mappingPolicy = policies.Get<IBuildKeyMappingPolicy>(buildKey); if (mappingPolicy != null) { return mappingPolicy.Map(buildKey, null).Type; } return buildKey.Type; }
/// <summary> /// Remove the resolvers for the given build key. /// </summary> /// <param name="policies">Policy list containing the build key.</param> /// <param name="buildKey">Build key.</param> public static void RemoveResolvers(IPolicyList policies, object buildKey) { IDependencyResolverTrackerPolicy tracker = policies.Get <IDependencyResolverTrackerPolicy>(buildKey); if (tracker != null) { tracker.RemoveResolvers(policies); } }
private static IBuilderPolicy GetPolicyForType(this IPolicyList list, Type policyInterface, Type buildType, out IPolicyList containingPolicyList) { if (buildType != null) { return(list.Get(policyInterface, buildType, out containingPolicyList)); } containingPolicyList = null; return(null); }
private static IBuilderPolicy GetPolicyForOpenGenericType(this IPolicyList list, Type policyInterface, Type buildType, out IPolicyList containingPolicyList) { if (buildType != null && buildType.GetTypeInfo().IsGenericType) { return(list.Get(policyInterface, buildType.GetGenericTypeDefinition(), out containingPolicyList)); } containingPolicyList = null; return(null); }
public static IBuilderPolicy Get(this IPolicyList policies, Type policyInterface, object buildKey, bool localOnly) { Guard.ArgumentNotNull(policies, "policies"); IPolicyList containingPolicyList; return(policies.Get(policyInterface, buildKey, localOnly, out containingPolicyList)); }
private Type GetMappedType(IPolicyList policies) { var mappingPolicy = policies.Get <IBuildKeyMappingPolicy>(this.buildKey); if (mappingPolicy != null) { return(mappingPolicy.Map(this.buildKey, null).Type); } return(this.buildKey.Type); }
// Helper methods for adding and removing the tracker policy. /// <summary> /// Get an instance that implements <see cref="IDependencyResolverTrackerPolicy"/>, /// either the current one in the policy set or creating a new one if it doesn't /// exist. /// </summary> /// <param name="policies">Policy list to look up from.</param> /// <param name="buildKey">Build key to track.</param> /// <returns>The resolver tracker.</returns> public static IDependencyResolverTrackerPolicy GetTracker(IPolicyList policies, object buildKey) { IDependencyResolverTrackerPolicy tracker = policies.Get<IDependencyResolverTrackerPolicy>(buildKey); if (tracker == null) { tracker = new DependencyResolverTrackerPolicy(); policies.Set<IDependencyResolverTrackerPolicy>(tracker, buildKey); } return tracker; }
/// <summary> /// GetOrDefault the list of behaviors for the current type so that it can be added to. /// </summary> /// <param name="policies">Policy list.</param> /// <param name="implementationType">Implementation type to set behaviors for.</param> /// <param name="name">Name type is registered under.</param> /// <returns>An instance of <see cref="InterceptionBehaviorsPolicy"/>.</returns> protected override InterceptionBehaviorsPolicy GetBehaviorsPolicy(IPolicyList policies, Type implementationType, string name) { var policy = policies.Get(implementationType, string.Empty, typeof(IInterceptionBehaviorsPolicy), out _); if (!(policy is InterceptionBehaviorsPolicy)) { policy = new InterceptionBehaviorsPolicy(); policies.Set(implementationType, string.Empty, typeof(IInterceptionBehaviorsPolicy), policy); } return((InterceptionBehaviorsPolicy)policy); }
private static SpecifiedMethodsSelectorPolicy GetSelectorPolicy(IPolicyList policies, Type typeToCreate, string name) { var selector = policies.Get(typeToCreate, name, typeof(IMethodSelectorPolicy), out _); if (!(selector is SpecifiedMethodsSelectorPolicy)) { selector = new SpecifiedMethodsSelectorPolicy(); policies.Set(typeToCreate, name, typeof(IMethodSelectorPolicy), selector); } return((SpecifiedMethodsSelectorPolicy)selector); }
// Helper methods for adding and removing the tracker policy. /// <summary> /// Get an instance that implements <see cref="IDependencyResolverTrackerPolicy"/>, /// either the current one in the policy set or creating a new one if it doesn't /// exist. /// </summary> /// <param name="policies">Policy list to look up from.</param> /// <param name="buildKey">Build key to track.</param> /// <returns>The resolver tracker.</returns> public static IDependencyResolverTrackerPolicy GetTracker(IPolicyList policies, object buildKey) { IDependencyResolverTrackerPolicy tracker = policies.Get <IDependencyResolverTrackerPolicy>(buildKey); if (tracker == null) { tracker = new DependencyResolverTrackerPolicy(); policies.Set <IDependencyResolverTrackerPolicy>(tracker, buildKey); } return(tracker); }
public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies) { var key = new NamedTypeBuildKey(implementationType, name); var policy = policies.Get<InterceptorPolicy>(key); if (policy == null) { policy = new InterceptorPolicy(); policies.Set(policy, key); } policy.AddInterceptor(this.interceptorContainer); }
public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies) { var key = new NamedTypeBuildKey(implementationType, name); var policy = policies.Get <InterceptorPolicy>(key); if (policy == null) { policy = new InterceptorPolicy(); policies.Set(policy, key); } policy.AddInterceptor(this.interceptorContainer); }
private Type GetLifetimeManagerType(IPolicyList policies) { var key = new NamedTypeBuildKey(MappedToType, Name); var lifetime = policies.Get<ILifetimePolicy>(key); if(lifetime != null) { return lifetime.GetType(); } if(MappedToType.IsGenericType) { var genericKey = new NamedTypeBuildKey(MappedToType.GetGenericTypeDefinition(), Name); var lifetimeFactory = policies.Get<ILifetimeFactoryPolicy>(genericKey); if(lifetimeFactory != null) { return lifetimeFactory.LifetimeType; } } return typeof (TransientLifetimeManager); }
private Type GetLifetimeManagerType(IPolicyList policies) { var key = new NamedTypeBuildKey(MappedToType, Name); var lifetime = policies.Get <ILifetimePolicy>(key); if (lifetime != null) { return(lifetime.GetType()); } if (MappedToType.GetTypeInfo().IsGenericType) { var genericKey = new NamedTypeBuildKey(MappedToType.GetGenericTypeDefinition(), Name); var lifetimeFactory = policies.Get <ILifetimeFactoryPolicy>(genericKey); if (lifetimeFactory != null) { return(lifetimeFactory.LifetimeType); } } return(typeof(TransientLifetimeManager)); }
private static SpecifiedPropertiesSelectorPolicy GetSelectorPolicy(IPolicyList policies, Type typeToInject, string name) { NamedTypeBuildKey key = new NamedTypeBuildKey(typeToInject, name); IPropertySelectorPolicy selector = (IPropertySelectorPolicy)policies.Get(typeToInject, name, typeof(IPropertySelectorPolicy), out _); if (!(selector is SpecifiedPropertiesSelectorPolicy)) { selector = new SpecifiedPropertiesSelectorPolicy(); policies.Set(key.Type, key.Name, typeof(IPropertySelectorPolicy), selector); } return((SpecifiedPropertiesSelectorPolicy)selector); }
private static IBuilderPolicy GetPolicyForOpenType(IPolicyList list, Type policyInterface, INamedType buildKey, Type buildType, out IPolicyList containingPolicyList) { containingPolicyList = null; if (null == buildType) { return(null); } if (buildType.GetTypeInfo().IsGenericType) { var newType = buildType.GetGenericTypeDefinition(); return(list.Get(newType, buildKey.Name, policyInterface, out containingPolicyList) ?? list.Get(newType, string.Empty, policyInterface, out containingPolicyList)); } if (buildType.IsArray && buildType.GetArrayRank() == 1) { return(list.Get(typeof(Array), buildKey.Name, policyInterface, out containingPolicyList) ?? list.Get(typeof(Array), string.Empty, policyInterface, out containingPolicyList)); } return(null); }
internal static InterceptionBehaviorsPolicy GetOrCreate( IPolicyList policies, Type typeToCreate, string name) { IInterceptionBehaviorsPolicy policy = (IInterceptionBehaviorsPolicy)policies.Get(typeToCreate, name, typeof(IInterceptionBehaviorsPolicy), out _); if (!(policy is InterceptionBehaviorsPolicy)) { policy = new InterceptionBehaviorsPolicy(); policies.Set(typeToCreate, name, typeof(IInterceptionBehaviorsPolicy), policy); } return((InterceptionBehaviorsPolicy)policy); }
internal static AdditionalInterfacesPolicy GetOrCreate(IPolicyList policies, Type typeToCreate, string name) { NamedTypeBuildKey key = new NamedTypeBuildKey(typeToCreate, name); IAdditionalInterfacesPolicy policy = (IAdditionalInterfacesPolicy)policies.Get(typeToCreate, name, typeof(IAdditionalInterfacesPolicy), out _); if (!(policy is AdditionalInterfacesPolicy)) { policy = new AdditionalInterfacesPolicy(); policies.Set(typeToCreate, name, typeof(IAdditionalInterfacesPolicy), policy); } return((AdditionalInterfacesPolicy)policy); }
public void InjectionConstructorInsertsChooserForDefaultConstructor() { InjectionConstructor ctor = new InjectionConstructor(); TestingBuilderContext context = new TestingBuilderContext(); IPolicyList policies = context.PersistentPolicies; ctor.AddPolicies(typeof(GuineaPig), policies); IConstructorSelectorPolicy selector = policies.Get <IConstructorSelectorPolicy>( new NamedTypeBuildKey(typeof(GuineaPig))); SelectedConstructor selected = selector.SelectConstructor(context); Assert.AreEqual(typeof(GuineaPig).GetConstructor(new Type[0]), selected.Constructor); Assert.AreEqual(0, selected.GetParameterKeys().Length); }
public IBuilderPolicy Get(Type type, string name, Type policyInterface, out IPolicyList list) { list = null; if (type != _store.Type || name != _store.Name) { return(_policies.Get(type, name, policyInterface, out list)); } var result = ((IPolicySet)_store).Get(policyInterface); if (null != result) { list = this; } return(result); }
public void InjectionConstructorInsertsChooserForDefaultConstructor() { var ctor = new InjectionConstructor(); var context = new MockBuilderContext { BuildKey = new NamedTypeBuildKey(typeof(GuineaPig)) }; IPolicyList policies = context.PersistentPolicies; ctor.AddPolicies(typeof(GuineaPig), policies); var selector = policies.Get <IConstructorSelectorPolicy>( new NamedTypeBuildKey(typeof(GuineaPig))); SelectedConstructor selected = selector.SelectConstructor(context, policies); Assert.Equal(typeof(GuineaPig).GetMatchingConstructor(new Type[0]), selected.Constructor); Assert.Equal(0, selected.GetParameterResolvers().Length); }
public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies) { Guard.AssertNotNull(implementationType, "implementationType"); Guard.AssertNotNull(policies, "policies"); ConstructorInfo ctor; InjectionParameterValue[] parameterValues; if (this.parameters.Count == 0) { ctor = implementationType.GetConstructor(Type.EmptyTypes); parameterValues = new InjectionParameterValue[0]; } else { NamedTypeBuildKey key = new NamedTypeBuildKey(implementationType, name); IParameterMatchingConventionsPolicy conventions = policies.Get <IParameterMatchingConventionsPolicy>(key); if (conventions == null) { conventions = new DefaultMatchingConventionsPolicy(); policies.SetDefault <IParameterMatchingConventionsPolicy>(conventions); } ctor = FindConstructor(implementationType, this.parameters, conventions); parameterValues = GetParameterValues(ctor, this.parameters, conventions); } IConstructorSelectorPolicy policy = new SpecifiedConstructorSelectorPolicy(ctor, parameterValues); NamedTypeBuildKey buildKey = new NamedTypeBuildKey(implementationType, name); policies.Set <IConstructorSelectorPolicy>(policy, buildKey); }
private static SpecifiedPropertiesSelectorPolicy GetSelectorPolicy(IPolicyList policies, Type typeToInject, string name) { NamedTypeBuildKey key = new NamedTypeBuildKey(typeToInject, name); IPropertySelectorPolicy selector = policies.GetNoDefault <IPropertySelectorPolicy>(key, false); if (selector == null) { FullAutowirePropertySelectorPolicy defaultSelector = policies.Get <IPropertySelectorPolicy>(key, false) as FullAutowirePropertySelectorPolicy; if (defaultSelector != null) { selector = defaultSelector.Clone(); policies.Set(selector, key); } else { throw new InvalidOperationException("Cannot use AutiviewEnabledInjectionProperty without FullAutowireContainerExtension. Please register FullAutowireContainerExtension extension in the container."); } } return(((FullAutowirePropertySelectorPolicy)selector).SpecifiedPropertiesPolicy); }
public void InjectionConstructorSetsResolverForInterfaceToLookupInContainer() { var ctor = new InjectionConstructor("Logger", typeof(ILogger)); var context = new MockBuilderContext(); context.BuildKey = new NamedTypeBuildKey(typeof(GuineaPig)); IPolicyList policies = context.PersistentPolicies; ctor.AddPolicies(typeof(GuineaPig), policies); var selector = policies.Get <IConstructorSelectorPolicy>( new NamedTypeBuildKey(typeof(GuineaPig))); SelectedConstructor selected = selector.SelectConstructor(context, policies); var resolvers = selected.GetParameterResolvers(); Assert.Equal(typeof(GuineaPig).GetMatchingConstructor(Sequence.Collect(typeof(string), typeof(ILogger))), selected.Constructor); Assert.Equal(2, resolvers.Length); var policy = resolvers[1]; Assert.True(policy is NamedTypeDependencyResolverPolicy); }
private static IBuilderPolicy GetDefaultForPolicy(this IPolicyList list, Type policyInterface, out IPolicyList containingPolicyList) { return(list.Get(policyInterface, null, out containingPolicyList)); }
public static T Get <T>(this IPolicyList policies, Type type, string name, out IPolicyList list) { return((T)policies.Get(type, name, typeof(T), out list)); }
private static SpecifiedPropertiesSelectorPolicy GetSelectorPolicy(IPolicyList policies, Type typeToInject, string name) { NamedTypeBuildKey key = new NamedTypeBuildKey(typeToInject, name); IPropertySelectorPolicy selector = policies.GetNoDefault<IPropertySelectorPolicy>(key, false); if (selector == null) { FullAutowirePropertySelectorPolicy defaultSelector = policies.Get<IPropertySelectorPolicy>(key, false) as FullAutowirePropertySelectorPolicy; if (defaultSelector != null) { selector = defaultSelector.Clone(); policies.Set(selector, key); } else { throw new InvalidOperationException("Cannot use AutiviewEnabledInjectionProperty without FullAutowireContainerExtension. Please register FullAutowireContainerExtension extension in the container."); } } return ((FullAutowirePropertySelectorPolicy)selector).SpecifiedPropertiesPolicy; }
private object ResolveValue(IPolicyList policies, string key) { IDependencyResolverPolicy resolver = policies.Get<IDependencyResolverPolicy>(key); return resolver.Resolve(null); }
public static TPolicyInterface GetPolicy <TPolicyInterface>(IPolicyList list, INamedType buildKey, out IPolicyList containingPolicyList) { return((TPolicyInterface)(list.Get(buildKey.Type, buildKey.Name, typeof(TPolicyInterface), out containingPolicyList) ?? GetExtended(list, typeof(TPolicyInterface), buildKey, buildKey.Type, out containingPolicyList) ?? list.Get(null, null, typeof(TPolicyInterface), out containingPolicyList))); // Nothing! Get Default }
private LifetimeManager GetLifetimeManager(IPolicyList policies) { var key = new NamedTypeBuildKey(MappedToType, Name); return (LifetimeManager)policies.Get<ILifetimePolicy>(key); }