public void CreationPolicyWillRecordSingletonsUsingLocalLifetimeContainerOnly()
        {
            BuilderStrategyChain chain = new BuilderStrategyChain();

            chain.Add(new CreationStrategy());

            Locator           parentLocator = new Locator();
            LifetimeContainer container     = new LifetimeContainer();

            parentLocator.Add(typeof(ILifetimeContainer), container);

            Locator childLocator = new Locator(parentLocator);

            PolicyList policies = new PolicyList();

            policies.SetDefault <ICreationPolicy>(new DefaultCreationPolicy());
            policies.SetDefault <ISingletonPolicy>(new SingletonPolicy(true));

            BuilderContext ctx = new BuilderContext(chain, childLocator, policies);

            object obj = ctx.HeadOfChain.BuildUp(ctx, typeof(object), null, null);

            Assert.IsNotNull(obj);
            Assert.IsNull(childLocator.Get(new DependencyResolutionLocatorKey(typeof(object), null)));
        }
示例#2
0
        private static IPlanBuilderPolicy CreatePlanBuilder()
        {
            BuilderStrategyChain chain = new BuilderStrategyChain();

            chain.Add(new CallConstructorStrategy());
            chain.Add(new SetPropertiesStrategy());
            chain.Add(new CallMethodsStrategy());

            PolicyList policies = new PolicyList();

            policies.SetDefault <IConstructorChooserPolicy>(new AttributeBasedConstructorChooser());
            policies.SetDefault <IPropertyChooserPolicy>(new AttributeBasedPropertyChooser());
            policies.SetDefault <IMethodChooserPolicy>(new AttributeBasedMethodChooser());

            return(new DynamicMethodPlanBuilderPolicy(chain, policies));
        }
示例#3
0
        /// <summary>
        /// Builds an object with the given <c>id</c>, and makes it a singleton
        /// in the container, which also manages its lifetime.
        /// </summary>
        /// <typeparam name="TTypeToBuild">The type of the object to build.</typeparam>
        /// <param name="objectId">The <c>id</c> of the object to build.</param>
        /// <exception cref="ArgumentNullException"><paramref name="objectId"/> is null.</exception>
        public TTypeToBuild BuildUp <TTypeToBuild>(string objectId)
        {
            Guard.ArgumentNotNull(objectId, "objectId");
            PolicyList policies = new PolicyList();

            policies.SetDefault <ISingletonPolicy>(new SingletonPolicy(true));

            return(builder.BuildUp <TTypeToBuild>(locator, objectId, null, policies));
        }
示例#4
0
        public void DefaultPolicyUsedWhenSpecificPolicyIsntAvailable()
        {
            PolicyList list          = new PolicyList();
            MockPolicy defaultPolicy = new MockPolicy();

            list.SetDefault <IBuilderPolicy>(defaultPolicy);

            Assert.AreSame(defaultPolicy, list.Get <IBuilderPolicy>(typeof(object), null));
        }
示例#5
0
        public void CanClearDefaultPolicy()
        {
            PolicyList list = new PolicyList();
            MockPolicy defaultPolicy = new MockPolicy();
            list.SetDefault<IBuilderPolicy>(defaultPolicy);

            list.ClearDefault<IBuilderPolicy>();

            Assert.IsNull(list.Get<IBuilderPolicy>(typeof(object), null));
        }
示例#6
0
        /// <summary>
        /// Applies the builder strategies to an existing object with the given <c>id</c>,
        /// and makes it a singleton in the container, which also manages its lifetime.
        /// </summary>
        /// <typeparam name="TTypeToBuild">The type being built, which may not be the
        /// concrete type of the object instance (i.e. an interface).</typeparam>
        /// <param name="existing">The existing object to apply strategies to.</param>
        /// <param name="objectId">The <c>id</c> of the object the strategies will be applied to.</param>
        /// <exception cref="ArgumentNullException"><paramref name="existing"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="objectId"/> is null.</exception>
        public void ApplyBuilder <TTypeToBuild>(TTypeToBuild existing, string objectId)
        {
            Guard.ArgumentNotNull(existing, "existing");
            Guard.ArgumentNotNull(objectId, "objectId");

            PolicyList policies = new PolicyList();

            policies.SetDefault <ISingletonPolicy>(new SingletonPolicy(true));

            builder.BuildUp <TTypeToBuild>(locator, objectId, existing, policies);
        }
        public void CreateInstanceCallDefaultExplicitConstructor()
        {
            PolicyList policies = new PolicyList();
            policies.SetDefault<IConstructorChooserPolicy>(new DefaultConstructorChooserPolicy());

            MockBuilderContext ctx = BuildContext(policies);

            MockObject mockInstance = (MockObject) ctx.HeadOfChain.BuildUp(ctx, typeof (MockObject), null, null);

            Assert.IsTrue(mockInstance.DefaultConstructorCalled);
        }
		public void CreateInstanceCallCreateInstance()
		{
			PolicyList policies = new PolicyList();
			policies.SetDefault<IConstructorChooserPolicy>(new MockReturnNullConstructor());

			MockBuilderContext ctx = BuildContext(policies);

			MockObject mockInstance = (MockObject) ctx.HeadOfChain.BuildUp(ctx, typeof (MockObject), null, null);

			Assert.IsTrue(mockInstance.DefaultConstructorCalled);
		}
示例#9
0
        public void DefaultPolicyUsedWhenSpecificPolicyIsntAvailable()
        {
            PolicyList list          = new PolicyList();
            FakePolicy defaultPolicy = new FakePolicy();

            list.SetDefault <IBuilderPolicy>(defaultPolicy);

            IBuilderPolicy result = list.Get <IBuilderPolicy>(typeof(object));

            Assert.AreSame(defaultPolicy, result);
        }
示例#10
0
        public void CanClearDefaultPolicy()
        {
            PolicyList list          = new PolicyList();
            MockPolicy defaultPolicy = new MockPolicy();

            list.SetDefault <IBuilderPolicy>(defaultPolicy);

            list.ClearDefault <IBuilderPolicy>();

            Assert.IsNull(list.Get <IBuilderPolicy>(typeof(object), null));
        }
示例#11
0
        public void SpecificPolicyOverridesDefaultPolicy()
        {
            PolicyList list           = new PolicyList();
            MockPolicy defaultPolicy  = new MockPolicy();
            MockPolicy specificPolicy = new MockPolicy();

            list.Set <IBuilderPolicy>(specificPolicy, typeof(object), null);
            list.SetDefault <IBuilderPolicy>(defaultPolicy);

            Assert.AreSame(specificPolicy, list.Get <IBuilderPolicy>(typeof(object), null));
        }
		public void CreateInstanceCallAttributedCtor()
		{
			PolicyList policies = new PolicyList();
			policies.SetDefault<IConstructorChooserPolicy>(new AttributeBasedConstructorChooser());

			MockBuilderContext ctx = BuildContext(policies);

			MockObjectWithAttributedCtor mockInstance =
				(MockObjectWithAttributedCtor) ctx.HeadOfChain.BuildUp(ctx, typeof (MockObjectWithAttributedCtor), null, null);

			Assert.IsTrue(mockInstance.AttributedConstructorCalled);
		}
示例#13
0
        public void WillUseInnerDefaultPolicyWhenOuterHasNoAnswer()
        {
            PolicyList innerList = new PolicyList();
            PolicyList outerList = new PolicyList(innerList);
            FakePolicy policy    = new FakePolicy();

            innerList.SetDefault(policy);

            FakePolicy result = outerList.Get <FakePolicy>(typeof(object));

            Assert.AreSame(policy, result);
        }
        private PolicyList CreatePolicyList(ConfigurationSection settings, bool applyInjectionPolicies)
        {
            DictionaryConfigurationSource configurationSource = new DictionaryConfigurationSource();

            configurationSource.Add(PolicyInjectionSettings.SectionName, settings);

            PolicyList policyList = new PolicyList();

            policyList.SetDefault <IPolicyInjectionPolicy>(new PolicyInjectionPolicy(applyInjectionPolicies));
            //policyList.Set<IPolicyInjectionPolicy>(new PolicyInjectionPolicy(false),
            //                                       typeof(ClassThatDependsOnIInterface));
            policyList.Set <IConfigurationObjectPolicy>(new ConfigurationObjectPolicy(configurationSource),
                                                        typeof(IConfigurationSource));

            policyList.SetDefault <IBuildPlanCreatorPolicy>(new DynamicMethodBuildPlanCreatorPolicy(buildPlanStrategyChain));
            policyList.SetDefault <IDynamicBuilderMethodCreatorPolicy>(new DefaultDynamicBuilderMethodCreatorPolicy());
            policyList.SetDefault <IConstructorSelectorPolicy>(new ConstructorSelectorPolicy <Attribute>());
            policyList.SetDefault <IPropertySelectorPolicy>(new PropertySelectorPolicy <Attribute>());

            return(policyList);
        }
		public void CreateInstanceWithNoConstructorAtAll()
		{
			PolicyList policies = new PolicyList();
			policies.SetDefault<IConstructorChooserPolicy>(new DefaultConstructorChooserPolicy());

			MockBuilderContext ctx = BuildContext(policies);

			int mockInstance = (int) ctx.HeadOfChain.BuildUp(ctx, typeof (int), null, null);

			//Int instances have zero as initialization value
			Assert.AreEqual((int) 0, mockInstance);
		}
        public void CreateInstanceCallAttributedCtor()
        {
            PolicyList policies = new PolicyList();
            policies.SetDefault<IConstructorChooserPolicy>(new AttributeBasedConstructorChooser());

            MockBuilderContext ctx = BuildContext(policies);

            MockObjectWithAttributedCtor mockInstance =
                (MockObjectWithAttributedCtor) ctx.HeadOfChain.BuildUp(ctx, typeof (MockObjectWithAttributedCtor), null, null);

            Assert.IsTrue(mockInstance.AttributedConstructorCalled);
        }
        public void BuildUpExecutesTwoParameterlessMethods()
        {
            PolicyList policies = new PolicyList();
            policies.SetDefault<IMethodChooserPolicy>(new AttributeBasedMethodChooser());
            MockBuilderContext ctx = BuildContext(policies);

            MockObject mockInstance = (MockObject) ctx.HeadOfChain.BuildUp(ctx, typeof (MockObject), null, null);

            Assert.IsTrue(mockInstance.InjectionMethod1Called);
            Assert.IsTrue(mockInstance.InjectionMethod2Called);
            Assert.IsFalse(mockInstance.Method3Called);
        }
        public void BuildUpExecutesTwoParameterlessMethods()
        {
            PolicyList policies = new PolicyList();

            policies.SetDefault <IMethodChooserPolicy>(new AttributeBasedMethodChooser());
            MockBuilderContext ctx = BuildContext(policies);

            MockObject mockInstance = (MockObject)ctx.HeadOfChain.BuildUp(ctx, typeof(MockObject), null, null);

            Assert.IsTrue(mockInstance.InjectionMethod1Called);
            Assert.IsTrue(mockInstance.InjectionMethod2Called);
            Assert.IsFalse(mockInstance.Method3Called);
        }
示例#19
0
        public void CanClearDefaultPolicy()
        {
            PolicyList list          = new PolicyList();
            FakePolicy defaultPolicy = new FakePolicy();

            list.SetDefault <IBuilderPolicy>(defaultPolicy);

            list.ClearDefault <IBuilderPolicy>();

            IBuilderPolicy result = list.Get <IBuilderPolicy>(typeof(object));

            Assert.IsNull(result);
        }
示例#20
0
        public void SpecificPolicyOverridesDefaultPolicy()
        {
            PolicyList list           = new PolicyList();
            FakePolicy defaultPolicy  = new FakePolicy();
            FakePolicy specificPolicy = new FakePolicy();

            list.Set <IBuilderPolicy>(specificPolicy, typeof(object));
            list.SetDefault <IBuilderPolicy>(defaultPolicy);

            IBuilderPolicy result = list.Get <IBuilderPolicy>(typeof(object));

            Assert.AreSame(specificPolicy, result);
        }
示例#21
0
        public void SpecificInnerPolicyOverridesDefaultOuterPolicy()
        {
            PolicyList innerList   = new PolicyList();
            PolicyList outerList   = new PolicyList(innerList);
            FakePolicy innerPolicy = new FakePolicy();
            FakePolicy outerPolicy = new FakePolicy();

            innerList.Set(innerPolicy, typeof(object));
            outerList.SetDefault(outerPolicy);

            FakePolicy result = outerList.Get <FakePolicy>(typeof(object));

            Assert.AreSame(innerPolicy, result);
        }
示例#22
0
        public void OuterPolicyDefaultOverridesInnerPolicyDefault()
        {
            PolicyList innerList   = new PolicyList();
            PolicyList outerList   = new PolicyList(innerList);
            FakePolicy innerPolicy = new FakePolicy();
            FakePolicy outerPolicy = new FakePolicy();

            innerList.SetDefault(innerPolicy);
            outerList.SetDefault(outerPolicy);

            IPolicyList containingPolicyList;
            FakePolicy  result = outerList.Get <FakePolicy>(typeof(object), out containingPolicyList);

            Assert.AreSame(outerPolicy, result);
            Assert.AreSame(outerList, containingPolicyList);
        }
        DependencyContainer(IReadableLocator innerLocator,
                            IPolicyList innerPolicies,
                            StagedStrategyChain <BuilderStage> innerStrategies,
                            IDependencyContainerConfigurator configurator)
        {
            locator    = new Locator(innerLocator);
            policies   = new PolicyList(innerPolicies);
            strategies = new StagedStrategyChain <BuilderStage>(innerStrategies);

            RegisterSingletonInstance <IObjectFactory>(this);

            if (innerStrategies == null)
            {
                strategies.AddNew <BuildKeyMappingStrategy>(BuilderStage.PreCreation);
                strategies.AddNew <SingletonStrategy>(BuilderStage.PreCreation);
                strategies.AddNew <ConstructorReflectionStrategy>(BuilderStage.PreCreation);
                strategies.AddNew <MethodReflectionStrategy>(BuilderStage.PreCreation);
                strategies.AddNew <PropertyReflectionStrategy>(BuilderStage.PreCreation);
                strategies.AddNew <EventBrokerReflectionStrategy>(BuilderStage.PreCreation);
                strategies.AddNew <InterceptionReflectionStrategy>(BuilderStage.PreCreation);

                strategies.AddNew <InterfaceInterceptionStrategy>(BuilderStage.Creation);
                strategies.AddNew <VirtualInterceptionStrategy>(BuilderStage.Creation);
                strategies.AddNew <CreationStrategy>(BuilderStage.Creation);

                strategies.AddNew <PropertySetterStrategy>(BuilderStage.Initialization);
                strategies.AddNew <MethodCallStrategy>(BuilderStage.Initialization);
                strategies.AddNew <EventBrokerStrategy>(BuilderStage.Initialization);

                strategies.AddNew <BuilderAwareStrategy>(BuilderStage.PostInitialization);
                strategies.AddNew <RemotingInterceptionStrategy>(BuilderStage.PostInitialization);
            }

            if (innerPolicies == null)
            {
                policies.SetDefault <ICreationPolicy>(new DefaultCreationPolicy());
            }

            locator.Add(typeof(EventBrokerService), new EventBrokerService());

            if (configurator != null)
            {
                configurator.Configure(this);
            }
        }
示例#24
0
        public void BuildUpAttributePropChooserSetPropertiesWithInheritedParameterAttributes()
        {
            PolicyList policies = new PolicyList();

            policies.SetDefault <IPropertyChooserPolicy>(new AttributeBasedPropertyChooser());

            MockBuilderContext ctx = BuildContext(policies);
            TestableRootCompositionContainer compositionContainer = new TestableRootCompositionContainer();

            ctx.Locator.Add(new DependencyResolutionLocatorKey(typeof(CompositionContainer), null), compositionContainer);
            MockDependentObject serviceDependency =
                compositionContainer.Services.AddNew <MockDependentObject, IMockDependentObject>();

            MockObject mockInstance = new MockObject();

            mockInstance = (MockObject)ctx.HeadOfChain.BuildUp(ctx, typeof(MockObject), null, null);

            Assert.IsNotNull(mockInstance.CreateNewProperty);
            Assert.AreSame(serviceDependency, mockInstance.ServiceDependencyProperty);
            Assert.IsNull(mockInstance.InternalProperty);
        }
 public void SetDefaultPolicy(Type policyType,
                              IBuilderPolicy policy)
 {
     policies.SetDefault(policyType, policy);
 }
示例#26
0
        public void SpecificPolicyOverridesDefaultPolicy()
        {
            PolicyList list = new PolicyList();
            MockPolicy defaultPolicy = new MockPolicy();
            MockPolicy specificPolicy = new MockPolicy();

            list.Set<IBuilderPolicy>(specificPolicy, typeof(object), null);
            list.SetDefault<IBuilderPolicy>(defaultPolicy);

            Assert.AreSame(specificPolicy, list.Get<IBuilderPolicy>(typeof(object), null));
        }
示例#27
0
        public void DefaultPolicyUsedWhenSpecificPolicyIsntAvailable()
        {
            PolicyList list = new PolicyList();
            MockPolicy defaultPolicy = new MockPolicy();

            list.SetDefault<IBuilderPolicy>(defaultPolicy);

            Assert.AreSame(defaultPolicy, list.Get<IBuilderPolicy>(typeof(object), null));
        }
        public void CreateInstanceWithNoConstructorAtAll()
        {
            PolicyList policies = new PolicyList();
            policies.SetDefault<IConstructorChooserPolicy>(new DefaultConstructorChooserPolicy());

            MockBuilderContext ctx = BuildContext(policies);

            int mockInstance = (int) ctx.HeadOfChain.BuildUp(ctx, typeof (int), null, null);

            //Int instances have zero as initialization value
            Assert.AreEqual((int) 0, mockInstance);
        }
示例#29
0
        public void CreationPolicyWillRecordSingletonsUsingLocalLifetimeContainerOnly()
        {
            BuilderStrategyChain chain = new BuilderStrategyChain();
            chain.Add(new CreationStrategy());

            Locator parentLocator = new Locator();
            LifetimeContainer container = new LifetimeContainer();
            parentLocator.Add(typeof(ILifetimeContainer), container);

            Locator childLocator = new Locator(parentLocator);

            PolicyList policies = new PolicyList();
            policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
            policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));

            BuilderContext ctx = new BuilderContext(chain, childLocator, policies);

            object obj = ctx.HeadOfChain.BuildUp(ctx, typeof(object), null, null);

            Assert.IsNotNull(obj);
            Assert.IsNull(childLocator.Get(new DependencyResolutionLocatorKey(typeof(object), null)));
        }