Exemple #1
0
        public object Resolve(IBuilderContext context)
        {
            var lifetimeManager = new ContainerControlledLifetimeManager();

            lifetimeManager.SetValue(_logSourceType);
            var loggerNameKey = new NamedTypeBuildKey(typeof(Type), "LogSourceType");

            //Create the build context for the logger
            var newKey = new NamedTypeBuildKey(_type, _name);

            //Register the item as a transient policy
            context.Policies.Set <IBuildKeyMappingPolicy>(new BuildKeyMappingPolicy(loggerNameKey), loggerNameKey);
            context.Policies.Set <ILifetimePolicy>(lifetimeManager, loggerNameKey);
            context.Lifetime.Add(lifetimeManager);

            try
            {
                return(context.NewBuildUp(newKey));
            }
            finally
            {
                context.Lifetime.Remove(lifetimeManager);
                context.Policies.Clear <IBuildKeyMappingPolicy>(loggerNameKey);
                context.Policies.Clear <ILifetimePolicy>(loggerNameKey);
            }
        }
Exemple #2
0
        private void SetSingleton(IBuilderContext context, Type t, object value)
        {
            var policy = new ContainerControlledLifetimeManager();

            policy.SetValue(value);
            context.PersistentPolicies.Set <ILifetimePolicy>(policy, new NamedTypeBuildKey(t));
        }
Exemple #3
0
        public void ComplicatedRegistrationsWithChildContainerLifetimes2()
        {
            var container = new QuickInjectContainer();

            container.AddBuildPlanVisitor(new TransientLifetimeRemovalBuildPlanVisitor());
            var child = container.CreateChildContainer();

            var correctInstanceForIFooResolutionFromChild = new Foo();
            var correctInstanceForFooResolutionFromChild  = new SuperFoo();

            var      preSetFooOnLifetime          = new Foo();
            SuperFoo fooResolvedFromMainContainer = new SuperFoo();

            var lifetime = new ContainerControlledLifetimeManager();

            lifetime.SetValue(fooResolvedFromMainContainer);

            container.RegisterType <IFoo, Foo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => new Foo()));
            container.RegisterType <IBar, Foo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => correctInstanceForIFooResolutionFromChild));
            container.RegisterType <Foo, SuperFoo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => fooResolvedFromMainContainer));
            container.RegisterType <SuperFoo>(lifetime);
            child.RegisterType <Foo, SuperFoo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => correctInstanceForFooResolutionFromChild));

            var f = container.Resolve <Foo>();
            var g = container.Resolve <Foo>();

            Assert.AreSame(child.Resolve <IBar>(), correctInstanceForIFooResolutionFromChild);
            Assert.AreSame(child.Resolve <IFoo>(), correctInstanceForIFooResolutionFromChild);
            Assert.AreSame(child.Resolve <Foo>(), correctInstanceForFooResolutionFromChild);
            Assert.AreSame(container.Resolve <Foo>(), fooResolvedFromMainContainer);
            Assert.AreSame(container.Resolve <SuperFoo>(), fooResolvedFromMainContainer);
        }
Exemple #4
0
        public void RegisterType(Type interfaceType, Type realType, object[] parms,
                                 ContainerControlledLifetimeManager containerControlledLifetimeManager)
        {
            var instance = Activator.CreateInstance(realType, parms);

            containerControlledLifetimeManager.SetValue(instance);
            objects.Add(interfaceType, containerControlledLifetimeManager);
        }
        private void AddExplicitBehaviorPolicies(Type implementationType, string name, IPolicyList policies)
        {
            var lifetimeManager = new ContainerControlledLifetimeManager();
            lifetimeManager.SetValue(explicitBehavior);
            var behaviorName = Guid.NewGuid().ToString();
            var newBehaviorKey = new NamedTypeBuildKey(explicitBehavior.GetType(), behaviorName);

            policies.Set<ILifetimePolicy>(lifetimeManager, newBehaviorKey);

            InterceptionBehaviorsPolicy behaviorsPolicy = GetBehaviorsPolicy(policies, implementationType, name);
            behaviorsPolicy.AddBehaviorKey(newBehaviorKey);
        }
        private void AddExplicitBehaviorPolicies(Type implementationType, string name, IPolicyList policies)
        {
            var lifetimeManager = new ContainerControlledLifetimeManager();

            lifetimeManager.SetValue(this.explicitBehavior);
            var behaviorName   = Guid.NewGuid().ToString();
            var newBehaviorKey = new NamedTypeBuildKey(this.explicitBehavior.GetType(), behaviorName);

            policies.Set <ILifetimePolicy>(lifetimeManager, newBehaviorKey);

            InterceptionBehaviorsPolicy behaviorsPolicy = this.GetBehaviorsPolicy(policies, implementationType, name);

            behaviorsPolicy.AddBehaviorKey(newBehaviorKey);
        }
Exemple #7
0
        public void LifetimeManagerWillProvideValueForAnInterfaceType()
        {
            var container = new QuickInjectContainer();

            container.AddBuildPlanVisitor(new TransientLifetimeRemovalBuildPlanVisitor());

            var lifetime = new ContainerControlledLifetimeManager();
            var foo      = new Foo();

            lifetime.SetValue(foo);

            container.RegisterType <IFoo>(lifetime);

            var a = container.Resolve <IFoo>();

            Assert.AreSame(a, foo);
        }
Exemple #8
0
        /// <summary>
        /// Add policies to the <paramref name="policies"/> to configure the container to use the represented
        /// <see cref="IInterceptionBehavior"/> for the supplied parameters.
        /// </summary>
        /// <param name="serviceType">Interface being registered.</param>
        /// <param name="implementationType">Type to register.</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(Type serviceType, Type implementationType, string name, IPolicyList policies)
        {
            if (_explicitBehavior != null)
            {
                var lifetimeManager = new ContainerControlledLifetimeManager();
                lifetimeManager.SetValue(_explicitBehavior);
                var behaviorName   = Guid.NewGuid().ToString();
                var newBehaviorKey = new NamedTypeBuildKey(_explicitBehavior.GetType(), behaviorName);

                policies.Set(newBehaviorKey.Type, newBehaviorKey.Name, typeof(ILifetimePolicy), lifetimeManager);
                InterceptionBehaviorsPolicy behaviorsPolicy = GetBehaviorsPolicy(policies, serviceType, name);
                behaviorsPolicy.AddBehaviorKey(newBehaviorKey);
            }
            else
            {
                var behaviorsPolicy = GetBehaviorsPolicy(policies, serviceType, name);
                behaviorsPolicy.AddBehaviorKey(_behaviorKey);
            }
        }
Exemple #9
0
        public void RegisterTypeAfterRegisterInstanceDoesNotReusePreviousInstance()
        {
            var container = new QuickInjectContainer();

            var foo  = new Foo();
            var foo2 = new Foo();

            container.RegisterInstance <IFoo>(foo);

            var returnedInstance = container.Resolve <IFoo>();

            var lifetime = new ContainerControlledLifetimeManager();

            lifetime.SetValue(foo2);

            container.RegisterType <IFoo>(lifetime);
            var returnedInstance2 = container.Resolve <IFoo>();

            Assert.AreNotSame(returnedInstance, returnedInstance2);
        }
        public void CanResolveSimpleParameterTypes()
        {
            MockBuilderContext context = GetContext();
            var key            = new NamedTypeBuildKey <FileLogger>();
            var lifetimePolicy = new ContainerControlledLifetimeManager();

            lifetimePolicy.SetValue("C:\\Log.txt");
            context.Policies.Set <ILifetimePolicy>(lifetimePolicy, new NamedTypeBuildKey <string>());

            IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, key);

            context.BuildKey = key;

            plan.BuildUp(context);
            object     result = context.Existing;
            FileLogger logger = result as FileLogger;

            Assert.IsNotNull(result);
            Assert.IsNotNull(logger);
            Assert.AreEqual("C:\\Log.txt", logger.LogFile);
        }
    public override void PreBuildUp(IBuilderContext context)
    {
        var key = (NamedTypeBuildKey)context.OriginalBuildKey;

        if (factory.CanCreate(key.Type) && context.Existing == null)
        {
            context.Existing = factory.Create(key.Type);
            var ltm = new ContainerControlledLifetimeManager();
            ltm.SetValue(context.Existing);
            // Find the container to add this to
            IPolicyList parentPolicies;
            var         parentMarker = context.Policies.Get <ParentMarkerPolicy>(new NamedTypeBuildKey <ParentMarkerPolicy>(), out parentPolicies);
            // TODO: add error check - if policy is missing, extension is misconfigured
            // Add lifetime manager to container
            parentPolicies.Set <ILifetimePolicy>(ltm, new NamedTypeBuildKey(key.Type));
            // And add to LifetimeContainer so it gets disposed
            parentMarker.AddToLifetime(ltm);
            // Short circuit the rest of the chain, object's already created
            context.BuildComplete = true;
        }
    }
Exemple #12
0
        public void CanInjectProperties()
        {
            MockBuilderContext context        = GetContext();
            object             existingObject = new object();
            var lifetimePolicy = new ContainerControlledLifetimeManager();

            lifetimePolicy.SetValue(existingObject);
            context.Policies.Set <ILifetimePolicy>(lifetimePolicy, new NamedTypeBuildKey <object>());

            IBuildPlanPolicy plan =
                GetPlanCreator(context).CreatePlan(context, new NamedTypeBuildKey(typeof(OnePropertyClass)));

            OnePropertyClass existing = new OnePropertyClass();

            context.Existing = existing;
            context.BuildKey = new NamedTypeBuildKey(typeof(OnePropertyClass));
            plan.BuildUp(context);

            Assert.NotNull(existing.Key);
            Assert.Same(existingObject, existing.Key);
        }
Exemple #13
0
        public override void PreBuildUp(IBuilderContext context)
        {
            var key = (INamedType)context.OriginalBuildKey;

            //var key = (NamedTypeBuildKey)context.OriginalBuildKey;

            if (context.Existing == null)
            {
                // invoke CoreServerConfig.GetService<T>() using reflection
                MethodInfo method = typeof(ApplicationConfig).GetMethod("GetService")
                                    .MakeGenericMethod(new Type[] { key.Type });

                // get T from CoreServerConfig.GetService<T>()
                var diRegistartion = method.Invoke(this, new object[] { });
                context.Existing = diRegistartion;

                // TODO:    ContainerControlledLifetimeManagerr is breaking RESOLVE operation when generic interface types are getting added.
                //          so choosing SingletonLifetimeManager. assuming there won't be any implications.
                var ltm = new ContainerControlledLifetimeManager();
                //var ltm = new SingletonLifetimeManager();
                ltm.SetValue(context.Existing);

                // Find the container to add this to
                IPolicyList parentPolicies;
                var         parentMarker = context.Policies.Get <ParentMarkerPolicy>(new NamedTypeBuildKey <ParentMarkerPolicy>(), out parentPolicies);

                // TODO: add error check - if policy is missing, extension is misconfigured

                // Add lifetime manager to container
                parentPolicies.Set <ILifetimePolicy>(ltm, new NamedTypeBuildKey(key.Type));
                // And add to LifetimeContainer so it gets disposed
                parentMarker.AddToLifetime(ltm);

                // Short circuit the rest of the chain, object's already created
                context.BuildComplete = true;
            }
        }