private ILifetimePolicy GetLifetimePolicyForGenericType(IBuilderContext context, out IPolicyList factorySource)
        {
            var    typeToBuild         = context.OriginalBuildKey.Type;
            object openGenericBuildKey = new NamedTypeBuildKey(typeToBuild.GetGenericTypeDefinition(),
                                                               context.BuildKey.Name);

            var factoryPolicy = context.Policies
                                .Get <ILifetimeFactoryPolicy>(openGenericBuildKey, out factorySource);

            if (factoryPolicy != null)
            {
                // creating the lifetime policy can result in arbitrary code execution
                // in particular it will likely result in a Resolve call, which could result in locking
                // to avoid deadlocks the new lifetime policy is created outside the lock
                // multiple instances might be created, but only one instance will be used
                ILifetimePolicy newLifetime = factoryPolicy.CreateLifetimePolicy();

                lock (_genericLifetimeManagerLock)
                {
                    // check whether the policy for closed-generic has been added since first checked
                    var lifetime = factorySource.GetNoDefault <ILifetimePolicy>(context.BuildKey);
                    if (lifetime == null && !(newLifetime is TransientLifetimeManager))
                    {
                        factorySource.Set(newLifetime, context.BuildKey);
                    }

                    lifetime = newLifetime;

                    return(lifetime);
                }
            }

            return(null);
        }
Esempio n. 2
0
        public static TPolicyInterface GetNoDefault <TPolicyInterface>(this IPolicyList policies, object buildKey,
                                                                       bool localOnly, out IPolicyList containingPolicyList)
            where TPolicyInterface : IBuilderPolicy
        {
            Guard.ArgumentNotNull(policies, "policies");

            return((TPolicyInterface)policies.GetNoDefault(typeof(TPolicyInterface), buildKey, localOnly, out containingPolicyList));
        }
Esempio n. 3
0
        /// <summary>
        /// Get the non default 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 GetNoDefault(this IPolicyList policies, Type policyInterface,
                                                  object buildKey,
                                                  bool localOnly)
        {
            IPolicyList containingPolicyList;

            return(policies.GetNoDefault(policyInterface, buildKey, localOnly, out containingPolicyList));
        }
 /// <summary>
 /// Get 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.GetNoDefault<IInterceptionBehaviorsPolicy>(implementationType, false);
     if ((policy == null) || !(policy is InterceptionBehaviorsPolicy))
     {
         policy = new InterceptionBehaviorsPolicy();
         policies.Set(policy, implementationType);
     }
     return (InterceptionBehaviorsPolicy) policy;
 }
 /// <summary>
 /// Get 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.GetNoDefault<IInterceptionBehaviorsPolicy>(implementationType, false);
     if ((policy == null) || !(policy is InterceptionBehaviorsPolicy))
     {
         policy = new InterceptionBehaviorsPolicy();
         policies.Set(policy, implementationType);
     }
     return (InterceptionBehaviorsPolicy) policy;
 }
Esempio n. 6
0
        private static SpecifiedMethodsSelectorPolicy GetSelectorPolicy(IPolicyList policies, Type typeToCreate, string name)
        {
            var key      = new NamedTypeBuildKey(typeToCreate, name);
            var selector = policies.GetNoDefault <IMethodSelectorPolicy>(key, false);

            if (selector == null || !(selector is SpecifiedMethodsSelectorPolicy))
            {
                selector = new SpecifiedMethodsSelectorPolicy();
                policies.Set <IMethodSelectorPolicy>(selector, key);
            }
            return((SpecifiedMethodsSelectorPolicy)selector);
        }
Esempio n. 7
0
        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 || !(selector is SpecifiedPropertiesSelectorPolicy))
            {
                selector = new SpecifiedPropertiesSelectorPolicy();
                policies.Set <IPropertySelectorPolicy>(selector, key);
            }
            return((SpecifiedPropertiesSelectorPolicy)selector);
        }
 internal static InterceptionBehaviorsPolicy GetOrCreate(
     IPolicyList policies,
     Type typeToCreate,
     string name)
 {
     NamedTypeBuildKey key = new NamedTypeBuildKey(typeToCreate, name);
     IInterceptionBehaviorsPolicy policy =
         policies.GetNoDefault<IInterceptionBehaviorsPolicy>(key, false);
     if ((policy == null) || !(policy is InterceptionBehaviorsPolicy))
     {
         policy = new InterceptionBehaviorsPolicy();
         policies.Set<IInterceptionBehaviorsPolicy>(policy, key);
     }
     return (InterceptionBehaviorsPolicy)policy;
 }
 internal static AdditionalInterfacesPolicy GetOrCreate(
     IPolicyList policies,
     Type typeToCreate,
     string name)
 {
     NamedTypeBuildKey key = new NamedTypeBuildKey(typeToCreate, name);
     IAdditionalInterfacesPolicy policy =
         policies.GetNoDefault<IAdditionalInterfacesPolicy>(key, false);
     if ((policy == null) || !(policy is AdditionalInterfacesPolicy))
     {
         policy = new AdditionalInterfacesPolicy();
         policies.Set<IAdditionalInterfacesPolicy>(policy, key);
     }
     return (AdditionalInterfacesPolicy)policy;
 }
        internal static AdditionalInterfacesPolicy GetOrCreate(
            IPolicyList policies,
            Type typeToCreate,
            string name)
        {
            NamedTypeBuildKey           key    = new NamedTypeBuildKey(typeToCreate, name);
            IAdditionalInterfacesPolicy policy =
                policies.GetNoDefault <IAdditionalInterfacesPolicy>(key, false);

            if ((policy == null) || !(policy is AdditionalInterfacesPolicy))
            {
                policy = new AdditionalInterfacesPolicy();
                policies.Set <IAdditionalInterfacesPolicy>(policy, key);
            }
            return((AdditionalInterfacesPolicy)policy);
        }
Esempio n. 11
0
        internal static InterceptionBehaviorsPolicy GetOrCreate(
            IPolicyList policies,
            Type typeToCreate,
            string name)
        {
            NamedTypeBuildKey            key    = new NamedTypeBuildKey(typeToCreate, name);
            IInterceptionBehaviorsPolicy policy =
                policies.GetNoDefault <IInterceptionBehaviorsPolicy>(key, false);

            if (policy == null || !(policy is InterceptionBehaviorsPolicy))
            {
                policy = new InterceptionBehaviorsPolicy();
                policies.Set(policy, key);
            }
            return((InterceptionBehaviorsPolicy)policy);
        }
Esempio n. 12
0
        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 static SpecifiedPropertiesSelectorPolicy GetSelectorPolicy(IPolicyList policies, Type typeToInject, string name)
        {
            var key = new NamedTypeBuildKey(typeToInject, name);
             var selector =
             policies.GetNoDefault<IPropertySelectorPolicy>(key, false);

             if (selector == null)
             {
            var 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;
        }
Esempio n. 14
0
 /// <summary>
 /// Get the non default policy.
 /// </summary>
 /// <typeparam name="TPolicyInterface">The interface the policy is registered under.</typeparam>
 /// <param name="policies"><see cref="IPolicyList"/> to search.</param>
 /// <param name="buildKey">The key the policy applies.</param>
 /// <param name="localOnly">true if the policy searches local only; otherwise false to search up the parent chain.</param>
 /// <returns>The policy in the list, if present; returns null otherwise.</returns>
 public static TPolicyInterface GetNoDefault <TPolicyInterface>(this IPolicyList policies, object buildKey,
                                                                bool localOnly)
     where TPolicyInterface : IBuilderPolicy
 {
     return((TPolicyInterface)policies.GetNoDefault(typeof(TPolicyInterface), buildKey, localOnly));
 }
Esempio n. 15
0
 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 || !(selector is SpecifiedPropertiesSelectorPolicy))
     {
         selector = new SpecifiedPropertiesSelectorPolicy();
         policies.Set<IPropertySelectorPolicy>(selector, key);
     }
     return (SpecifiedPropertiesSelectorPolicy)selector;
 }
Esempio n. 16
0
 private static SpecifiedMethodsSelectorPolicy GetSelectorPolicy(IPolicyList policies, Type typeToCreate, string name)
 {
     var key = new NamedTypeBuildKey(typeToCreate, name);
     var selector = policies.GetNoDefault<IMethodSelectorPolicy>(key, false);
     if (selector == null || !(selector is SpecifiedMethodsSelectorPolicy))
     {
         selector = new SpecifiedMethodsSelectorPolicy();
         policies.Set<IMethodSelectorPolicy>(selector, key);
     }
     return (SpecifiedMethodsSelectorPolicy)selector;
 }