/// <summary>
        /// A helper method used by the generated IL to set up a PerResolveLifetimeManager lifetime manager
        /// if the current object is such.
        /// </summary>
        /// <param name="context">Current build context.</param>
        public static void SetPerBuildSingleton(IBuilderContext context)
        {
            var lifetime = (context ?? throw  new ArgumentNullException(nameof(context))).Policies.Get <ILifetimePolicy>(context.OriginalBuildKey);

            if (lifetime is PerResolveLifetimeManager)
            {
                var perBuildLifetime = new InternalPerResolveLifetimeManager(context.Existing);
                context.Policies.Set <ILifetimePolicy>(perBuildLifetime, context.OriginalBuildKey);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// A helper method used by the generated IL to set up a PerResolveLifetimeManager lifetime manager
        /// if the current object is such.
        /// </summary>
        /// <param name="context">Current build context.</param>
        public static void SetPerBuildSingleton(IBuilderContext context)
        {
            var lifetime = (context ?? throw  new ArgumentNullException(nameof(context)))
                           .Policies.GetOrDefault(typeof(ILifetimePolicy), context.OriginalBuildKey, out _);

            if (lifetime is PerResolveLifetimeManager)
            {
                var perBuildLifetime = new InternalPerResolveLifetimeManager(context.Existing);
                context.Policies.Set(context.OriginalBuildKey.Type,
                                     context.OriginalBuildKey.Name,
                                     typeof(ILifetimePolicy), perBuildLifetime);
            }
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Add policies to the <paramref name="policies"/> to configure the
        /// container to call this constructor with the appropriate parameter values.
        /// </summary>
        /// <param name="registeredType">Type of interface being registered. If no interface,
        /// this will be null. This parameter is ignored in this implementation.</param>
        /// <param name="mappedToType">Type of concrete type being registered.</param>
        /// <param name="name">Name used to resolve the type object.</param>
        /// <param name="policies">Policy list to add policies to.</param>
        public override void AddPolicies <TContext, TPolicySet>(Type registeredType, Type mappedToType, string name, ref TPolicySet policies)
        {
            // Verify
            if (null != mappedToType && mappedToType != registeredType)
            {
                throw new InvalidOperationException(
                          "Registration where both MappedToType and InjectionFactory are set is not supported");
            }

            // Check if Per Resolve lifetime is required
            var lifetime = policies.Get(typeof(LifetimeManager));

            if (lifetime is PerResolveLifetimeManager)
            {
                policies.Set(typeof(ResolveDelegate <TContext>), CreatePerResolveLegacyPolicy());
            }
            else
            {
                policies.Set(typeof(ResolveDelegate <TContext>), CreateLegacyPolicy());
            }

            // Factory methods

            ResolveDelegate <TContext> CreateLegacyPolicy()
            {
                return((ref TContext c) =>
                       _factoryFunc(c.Container, c.Type, c.Name) ??
                       throw new InvalidOperationException("Injection Factory must return valid object or throw an exception"));
            }

            ResolveDelegate <TContext> CreatePerResolveLegacyPolicy()
            {
                return((ref TContext context) =>
                {
                    var result = _factoryFunc(context.Container, context.Type, context.Name);
                    var perBuildLifetime = new InternalPerResolveLifetimeManager(result);

                    context.Set(context.Type, context.Name, typeof(LifetimeManager), perBuildLifetime);
                    return result;
                });
            }
        }