Exemple #1
0
        public static TPolicyInterface GetPolicy <TPolicyInterface>(ref BuilderContext context)
        {
            return((TPolicyInterface)
                   (context.Get(context.RegistrationType, context.Name, typeof(TPolicyInterface)) ?? (
#if NETCOREAPP1_0 || NETSTANDARD1_0
                        context.RegistrationType.GetTypeInfo().IsGenericType
#else
                        context.RegistrationType.IsGenericType
#endif
                ? context.Get(context.RegistrationType.GetGenericTypeDefinition(), context.Name, typeof(TPolicyInterface)) ??
                        context.Get(null, null, typeof(TPolicyInterface))
                : context.Get(null, null, typeof(TPolicyInterface)))));
        }
        /// <summary>
        /// Called during the chain of responsibility for a build operation.
        /// </summary>
        /// <param name="context">The context for the operation.</param>
        public override void PreBuildUp(ref BuilderContext context)
        {
            // Get resolver if already created
            var resolver = context.Registration.Get <ResolveDelegate <BuilderContext> >() ?? (ResolveDelegate <BuilderContext>)
                           GetGeneric(ref context, typeof(ResolveDelegate <BuilderContext>));

            if (null == resolver)
            {
                // Check if can create at all

#if NETCOREAPP1_0 || NETSTANDARD1_0
                if (!(context.Registration is ContainerRegistration) && context.RegistrationType.GetTypeInfo().IsGenericTypeDefinition)
#else
                if (!(context.Registration is ContainerRegistration) && context.RegistrationType.IsGenericTypeDefinition)
#endif
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                              "The type {0} is an open generic type. An open generic type cannot be resolved.",
                                                              context.RegistrationType.FullName), new InvalidRegistrationException());
                }
                else if (context.Type.IsArray && context.Type.GetArrayRank() > 1)
                {
                    var message = $"Invalid array {context.Type}. Only arrays of rank 1 are supported";
                    throw new ArgumentException(message, new InvalidRegistrationException());
                }

                // Get resolver factory
                var factory = context.Registration.Get <ResolveDelegateFactory>() ?? (ResolveDelegateFactory)(
                    context.Get(context.Type, UnityContainer.All, typeof(ResolveDelegateFactory)) ??
                    GetGeneric(ref context, typeof(ResolveDelegateFactory)) ??
                    context.Get(null, null, typeof(ResolveDelegateFactory)));

                // Create plan
                if (null != factory)
                {
                    resolver = factory(ref context);

                    context.Registration.Set(typeof(ResolveDelegate <BuilderContext>), resolver);
                    context.Existing = resolver(ref context);
                }
                else
                {
                    throw new ResolutionFailedException(context.Type, context.Name, $"Failed to find Resolve Delegate Factory for Type {context.Type}");
                }
            }
            else
            {
                // Plan has been already created, just build the object
                context.Existing = resolver(ref context);
            }
        }
Exemple #3
0
        public override void PreBuildUp(ref BuilderContext context)
        {
            LifetimeManager policy = null;

            if (context.Registration is ImplicitRegistration registration)
            {
                policy = registration.LifetimeManager;
            }

            if (null == policy || policy is PerResolveLifetimeManager)
            {
                policy = (LifetimeManager)context.Get(typeof(LifetimeManager));
            }

            if (null == policy)
            {
                return;
            }

            if (policy is SynchronizedLifetimeManager recoveryPolicy)
            {
                context.RequiresRecovery = recoveryPolicy;
            }

            var existing = policy.GetValue(context.Lifetime);

            if (LifetimeManager.NoValue != existing)
            {
                context.Existing      = existing;
                context.BuildComplete = true;
            }
        }
        protected static object GetGeneric(ref BuilderContext context, Type policyInterface)
        {
            if (context.Registration is ContainerRegistration registration && null != context.Type)
            {
                // Check if generic
#if NETCOREAPP1_0 || NETSTANDARD1_0
                if (context.Type.GetTypeInfo().IsGenericType)
#else
                if (context.Type.IsGenericType)
#endif
                {
                    var newType = context.Type.GetGenericTypeDefinition();
                    return(context.Get(newType, context.Name, policyInterface) ??
                           context.Get(newType, UnityContainer.All, policyInterface));
                }
            }
Exemple #5
0
        protected static object GetGeneric(ref BuilderContext context, Type policyInterface, Type type, string name)
        {
            // Check if generic
#if NETCOREAPP1_0 || NETSTANDARD1_0
            if (type.GetTypeInfo().IsGenericType)
#else
            if (type.IsGenericType)
#endif
            {
                var newType = type.GetGenericTypeDefinition();
                return(context.Get(newType, name, policyInterface) ??
                       context.Get(newType, UnityContainer.All, policyInterface));
            }

            return(null);
        }
Exemple #6
0
        private void UpdateSpyPolicy(ref BuilderContext context)
        {
            SpyPolicy policy = (SpyPolicy)context.Get(null, null, typeof(SpyPolicy));

            if (policy != null)
            {
                policy.WasSpiedOn = true;
            }
        }
Exemple #7
0
        public override void PostBuildUp(ref BuilderContext context)
        {
            LifetimeManager policy = null;

            if (context.Registration is ContainerRegistration registration)
            {
                policy = registration.LifetimeManager;
            }

            if (null == policy || policy is PerResolveLifetimeManager)
            {
                policy = (LifetimeManager)context.Get(typeof(LifetimeManager));
            }

            policy?.SetValue(context.Existing, context.Lifetime);
        }
Exemple #8
0
        private static object ResolverImplementation <T>(ref BuilderContext context)
        {
            var container = context.Container;
            var name      = context.Name;

            context.Existing = new Lazy <T>(() => container.Resolve <T>(name));

            var lifetime = context.Get(typeof(LifetimeManager));

            if (lifetime is PerResolveLifetimeManager)
            {
                var perBuildLifetime = new InternalPerResolveLifetimeManager(context.Existing);
                context.Set(typeof(LifetimeManager), perBuildLifetime);
            }

            return(context.Existing);
        }
Exemple #9
0
        public override void PreBuildUp(ref BuilderContext context)
        {
            LifetimeManager policy = null;

            if (context.Registration is ContainerRegistration registration)
            {
                policy = registration.LifetimeManager;
            }

            if (null == policy || policy is PerResolveLifetimeManager)
            {
                policy = (LifetimeManager)context.Get(typeof(LifetimeManager));
            }
            if (null == policy)
            {
#if NETSTANDARD1_0 || NETCOREAPP1_0
                if (!context.RegistrationType.GetTypeInfo().IsGenericType)
                {
                    return;
                }
#else
                if (!context.RegistrationType.IsGenericType)
                {
                    return;
                }
#endif
                var manager = (LifetimeManager)context.Get(context.Type.GetGenericTypeDefinition(),
                                                           context.Name, typeof(LifetimeManager));
                if (null == manager)
                {
                    return;
                }

                lock (_genericLifetimeManagerLock)
                {
                    // check whether the policy for closed-generic has been added since first checked
                    policy = (LifetimeManager)context.Registration.Get(typeof(LifetimeManager));
                    if (null == policy)
                    {
                        policy = manager.CreateLifetimePolicy();
                        context.Registration.Set(typeof(LifetimeManager), policy);

                        if (policy is IDisposable)
                        {
                            var scope = policy is ContainerControlledLifetimeManager container
                                      ? ((UnityContainer)container.Scope)?.LifetimeContainer ?? context.Lifetime
                                      : context.Lifetime;
                            scope.Add(policy);
                        }
                    }
                }
            }

            if (policy is SynchronizedLifetimeManager recoveryPolicy)
            {
                context.RequiresRecovery = recoveryPolicy;
            }

            var existing = policy.GetValue(context.Lifetime);
            if (LifetimeManager.NoValue != existing)
            {
                context.Existing      = existing;
                context.BuildComplete = true;
            }
        }
 protected static TPolicyInterface Get_Policy <TPolicyInterface>(ref BuilderContext context, Type type, string name)
 {
     return((TPolicyInterface)(GetGeneric(ref context, typeof(TPolicyInterface), type, name) ??
                               context.Get(null, null, typeof(TPolicyInterface))));    // Nothing! Get Default
 }