示例#1
0
        LifetimeManager GetLifetimeManager(DependencyLifeTime lifeTime)
        {
            LifetimeManager lifetimeManager = null;

            switch (lifeTime)
            {
            case DependencyLifeTime.PerDependency:
            {
                lifetimeManager = new PerResolveLifetimeManager();
                break;
            }

            case DependencyLifeTime.PerRequest:
            {
                lifetimeManager = new PerRequestLifetimeManager();
                break;
            }

            case DependencyLifeTime.SingleInstance:
            {
                lifetimeManager = new ContainerControlledLifetimeManager();
                break;
            }

            default:
            {
                lifetimeManager = new PerResolveLifetimeManager();
                break;
            }
            }
            return(lifetimeManager);
        }
示例#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)
        {
            Guard.ArgumentNotNull(context, "context");

            var lifetime = context.Policies.Get <ILifetimePolicy>(context.OriginalBuildKey);

            if (lifetime is PerResolveLifetimeManager)
            {
                var perBuildLifetime = new PerResolveLifetimeManager(context.Existing);
                context.Policies.Set <ILifetimePolicy>(perBuildLifetime, context.OriginalBuildKey);
            }
        }
示例#3
0
        static void TestPerResolveLifetime_Nested()
        {
            Console.WriteLine("Test PerResolveLifetimeManager_Nested");
            LifetimeTest.ResetCounter();
            using (var container = new UnityContainer())
            {
                var lifeManager = new PerResolveLifetimeManager();
                container.RegisterType <ILifetimeTest, LifetimeTest>(lifeManager);


                var obj1 = container.Resolve <Foo>();
                LifetimeTest.PrintCounter();
            }
            LifetimeTest.PrintCounter();
        }
示例#4
0
        LifetimeManager GetLifetimeManager(Type lifeTimeType)
        {
            LifetimeManager lifetimeManager;

            if (KnownTypes.SingleInstanceType.IsAssignableFrom(lifeTimeType))
            {
                lifetimeManager = new ContainerControlledLifetimeManager();
            }
            else if (KnownTypes.PerRequestType.IsAssignableFrom(lifeTimeType))
            {
                lifetimeManager = new PerRequestLifetimeManager();
            }
            else
            {
                lifetimeManager = new PerResolveLifetimeManager();
            }
            return(lifetimeManager);
        }
示例#5
0
        public void Register(Type i, Type c, ILifetimeManager lifetimeManager = null)
        {
            if (lifetimeManager == null)
            {
                lifetimeManager = new PerResolveLifetimeManager();
            }

            var dependencyModel = new DependencyModel
            {
                From               = i,
                To                 = c,
                LifetimeManager    = lifetimeManager.GetType(),
                LifetimeManagerKey = lifetimeManager.GetKey(),
                ResolvedType       = DobbyInstanceService.GetInstance(c)
            };

            _registeredTypes.Add(dependencyModel);
            lifetimeManager.Dispose();
        }
示例#6
0
        public IContainer RegisterType(Type from, Type to, string name, LifeTime lifeTime = LifeTime.PerResolve)
        {
            LifetimeManager manager;

            if (lifeTime != LifeTime.PerResolve)
            {
                if (lifeTime != LifeTime.Singleton)
                {
                    throw new NotImplementedException();
                }
            }
            else
            {
                manager = new PerResolveLifetimeManager();
                goto Label_0021;
            }
            manager = new ContainerControlledLifetimeManager();
Label_0021:
            this._container.RegisterType(from, to, name, manager, new InjectionMember[0]);
            return(this);
        }