/// <summary>
        /// 构建策略
        /// </summary>
        /// <param name="servicedesc">服务描述</param>
        /// <param name="issave">是否保存服务</param>
        /// <param name="parameters">服务传入参数</param>
        /// <returns></returns>
        public PolicyList[] BuildPolicy(ServiceDesc servicedesc, bool issave, object[] parameters)
        {
            PolicyList[] policies   = new PolicyList[1];
            PolicyList   policylist = new PolicyList();

            policies[0] = policylist;

            //构建影射策略
            TypeMappingPolicy typepolicy = new TypeMappingPolicy(servicedesc.ImpleType, servicedesc.Key);

            policylist.Set <ITypeMappingPolicy>(typepolicy, servicedesc.ServiceType, servicedesc.Key);

            //构建单一构建策略(只有创建了单一创建策略才能够Locate)
            if (issave)
            {
                SingletonPolicy singletonpolicy = new SingletonPolicy(true);
                policylist.Set <ISingletonPolicy>(singletonpolicy, servicedesc.ImpleType, servicedesc.Key);
            }

            //建立创建策略,可以添加参数
            if (parameters != null && parameters.Length != 0)
            {
                ConstructorPolicy constuctorpolicy = new ConstructorPolicy();
                for (int i = 0; i < parameters.Length; i++)
                {
                    constuctorpolicy.AddParameter(new ValueParameter(parameters[i].GetType(), parameters[i]));
                }
                policylist.Set <ICreationPolicy>(constuctorpolicy, servicedesc.ImpleType, servicedesc.Key);
            }
            return(policies);
        }
Exemple #2
0
        public void ExceptionsThrownInPreTeardownGetTranslated()
        {
            StrategyChain chain = new StrategyChain();

            chain.Add(new ThrowingStrategy("a"));
            chain.Add(new ThrowingStrategy("b"));

            PolicyList policies = new PolicyList();

            policies.Set <IShouldThrowPolicy>(new ShouldThrowPolicy(false, false, true, false), "a");
            policies.Set <IShouldThrowPolicy>(new ShouldThrowPolicy(false, true, false, false), "b");

            Builder builder = new Builder();

            try
            {
                builder.TearDown(null, null, policies, chain, new object());
                Assert.Fail("Expected exception did not occur");
            }
            catch (BuildFailedException ex)
            {
                Assert.AreEqual(typeof(ThrowingStrategy).Name, ex.ExecutingStrategyTypeName);
                Assert.AreEqual(1, ex.ExecutingStrategyIndex);
                Assert.IsInstanceOfType(ex.InnerException, typeof(Exception));
            }
        }
Exemple #3
0
        private static PolicyList GetPolicies(IConfigurationSource configurationSource)
        {
            PolicyList policyList = new PolicyList();

            policyList.Set <IConfigurationObjectPolicy>(new ConfigurationObjectPolicy(configurationSource), typeof(IConfigurationSource), null);
            policyList.Set <IReflectionCachePolicy>(new ReflectionCachePolicy(reflectionCache), typeof(IReflectionCachePolicy), null);

            return(policyList);
        }
Exemple #4
0
        public void CanClearAllPolicies()
        {
            PolicyList list = new PolicyList();
            list.Set<IBuilderPolicy>(new MockPolicy(), typeof(object), null);
            list.Set<IBuilderPolicy>(new MockPolicy(), typeof(object), "1");

            list.ClearAll();

            Assert.AreEqual(0, list.Count);
        }
Exemple #5
0
        public void CanClearAllPolicies()
        {
            PolicyList list = new PolicyList();

            list.Set <IBuilderPolicy>(new FakePolicy(), "1");
            list.Set <IBuilderPolicy>(new FakePolicy(), "2");

            list.ClearAll();

            Assert.AreEqual(0, list.Count);
        }
		public void SetOverwritesExistingPolicy()
		{
			PolicyList list = new PolicyList();
			MockPolicy policy1 = new MockPolicy();
			MockPolicy policy2 = new MockPolicy();

			list.Set<IBuilderPolicy>(policy1, typeof(string), "1");
			list.Set<IBuilderPolicy>(policy2, typeof(string), "1");

			Assert.AreSame(policy2, list.Get<IBuilderPolicy>(typeof(string), "1"));
		}
Exemple #7
0
        public void CanClearAllPolicies()
        {
            PolicyList list = new PolicyList();

            list.Set <IBuilderPolicy>(new MockPolicy(), typeof(object), null);
            list.Set <IBuilderPolicy>(new MockPolicy(), typeof(object), "1");

            list.ClearAll();

            Assert.AreEqual(0, list.Count);
        }
Exemple #8
0
        public void SetOverwritesExistingPolicy()
        {
            PolicyList list    = new PolicyList();
            MockPolicy policy1 = new MockPolicy();
            MockPolicy policy2 = new MockPolicy();

            list.Set <IBuilderPolicy>(policy1, typeof(string), "1");
            list.Set <IBuilderPolicy>(policy2, typeof(string), "1");

            Assert.AreSame(policy2, list.Get <IBuilderPolicy>(typeof(string), "1"));
        }
Exemple #9
0
        public void CanAddMultiplePoliciesToBagAndRetrieveThem()
        {
            PolicyList list    = new PolicyList();
            FakePolicy policy1 = new FakePolicy();
            FakePolicy policy2 = new FakePolicy();

            list.Set <IBuilderPolicy>(policy1, "1");
            list.Set <IBuilderPolicy>(policy2, "2");

            Assert.AreSame(policy1, list.Get <IBuilderPolicy>("1"));
            Assert.AreSame(policy2, list.Get <IBuilderPolicy>("2"));
        }
Exemple #10
0
        public void CanAddMultiplePoliciesToBagAndRetrieveThem()
        {
            PolicyList list = new PolicyList();
            MockPolicy policy1 = new MockPolicy();
            MockPolicy policy2 = new MockPolicy();

            list.Set<IBuilderPolicy>(policy1, typeof(object), "1");
            list.Set<IBuilderPolicy>(policy2, typeof(string), "2");

            Assert.AreSame(policy1, list.Get<IBuilderPolicy>(typeof(object), "1"));
            Assert.AreSame(policy2, list.Get<IBuilderPolicy>(typeof(string), "2"));
        }
Exemple #11
0
        public void CanAddMultiplePoliciesToBagAndRetrieveThem()
        {
            PolicyList list    = new PolicyList();
            MockPolicy policy1 = new MockPolicy();
            MockPolicy policy2 = new MockPolicy();

            list.Set <IBuilderPolicy>(policy1, typeof(object), "1");
            list.Set <IBuilderPolicy>(policy2, typeof(string), "2");

            Assert.AreSame(policy1, list.Get <IBuilderPolicy>(typeof(object), "1"));
            Assert.AreSame(policy2, list.Get <IBuilderPolicy>(typeof(string), "2"));
        }
Exemple #12
0
        public void SpecificGenericPolicyComesBeforeGenericPolicy()
        {
            PolicyList list           = new PolicyList();
            FakePolicy genericPolicy  = new FakePolicy();
            FakePolicy specificPolicy = new FakePolicy();

            list.Set(genericPolicy, typeof(IDummy <>));
            list.Set(specificPolicy, typeof(IDummy <int>));

            FakePolicy result = list.Get <FakePolicy>(typeof(IDummy <int>));

            Assert.AreSame(specificPolicy, result);
        }
Exemple #13
0
        public void SetOverwritesExistingPolicy()
        {
            PolicyList list    = new PolicyList();
            FakePolicy policy1 = new FakePolicy();
            FakePolicy policy2 = new FakePolicy();

            list.Set <IBuilderPolicy>(policy1, typeof(string));
            list.Set <IBuilderPolicy>(policy2, typeof(string));

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

            Assert.AreSame(policy2, result);
        }
Exemple #14
0
        public void PolicyForClosedGenericTypeOverridesPolicyForOpenType()
        {
            PolicyList list             = new PolicyList();
            FakePolicy openTypePolicy   = new FakePolicy();
            FakePolicy closedTypePolicy = new FakePolicy();

            list.Set <IBuilderPolicy>(openTypePolicy, typeof(IDummy <>));
            list.Set <IBuilderPolicy>(closedTypePolicy, typeof(IDummy <object>));

            IBuilderPolicy result = list.Get <IBuilderPolicy>(new NamedTypeBuildKey <IDummy <object> >("name"));

            Assert.AreSame(closedTypePolicy, result);
        }
Exemple #15
0
        public void SpecificGenericPolicyComesBeforeGenericPolicy()
        {
            PolicyList list           = new PolicyList();
            FakePolicy genericPolicy  = new FakePolicy();
            FakePolicy specificPolicy = new FakePolicy();

            list.Set(typeof(IDummy <>), string.Empty, typeof(FakePolicy), genericPolicy);
            list.Set(typeof(IDummy <int>), string.Empty, typeof(FakePolicy), specificPolicy);

            var result = list.GetOrDefault(typeof(FakePolicy), typeof(IDummy <int>), out _);

            Assert.AreSame(specificPolicy, result);
        }
Exemple #16
0
        public void PolicyForClosedGenericTypeOverridesPolicyForOpenType()
        {
            PolicyList list             = new PolicyList();
            FakePolicy openTypePolicy   = new FakePolicy();
            FakePolicy closedTypePolicy = new FakePolicy();

            list.Set(typeof(IDummy <>), string.Empty, typeof(IBuilderPolicy), openTypePolicy);
            list.Set(typeof(IDummy <object>), string.Empty, typeof(IBuilderPolicy), closedTypePolicy);

            IBuilderPolicy result = list.GetOrDefault(typeof(IBuilderPolicy), new NamedTypeBuildKey <IDummy <object> >("name"), out _);

            Assert.AreSame(closedTypePolicy, result);
        }
Exemple #17
0
        public void SpecificPolicyOverridesDefaultPolicy()
        {
            PolicyList list           = new PolicyList();
            FakePolicy defaultPolicy  = new FakePolicy();
            FakePolicy specificPolicy = new FakePolicy();

            list.Set(typeof(object), string.Empty, typeof(IBuilderPolicy), specificPolicy);
            list.Set(null, null, typeof(IBuilderPolicy), defaultPolicy);

            IBuilderPolicy result = list.GetOrDefault(typeof(IBuilderPolicy), typeof(object), out _);

            Assert.AreSame(specificPolicy, result);
        }
Exemple #18
0
        public void SetOverwritesExistingPolicy()
        {
            PolicyList list    = new PolicyList();
            FakePolicy policy1 = new FakePolicy();
            FakePolicy policy2 = new FakePolicy();

            list.Set(typeof(string), string.Empty, typeof(IBuilderPolicy), policy1);
            list.Set(typeof(string), string.Empty, typeof(IBuilderPolicy), policy2);

            IBuilderPolicy result = list.GetOrDefault(typeof(IBuilderPolicy), typeof(string), out _);

            Assert.AreSame(policy2, result);
        }
        /// <summary>
        /// Utility method to initialize an object instance without adding it to the container.
        /// </summary>
        /// <param name="builder">The <see cref="IBuilder{TStageEnum}"/> to use to build the object.</param>
        /// <param name="locator">The <see cref="IReadWriteLocator"/> to use in building the object.</param>
        /// <param name="item">The object to build.</param>
        /// <returns>The built and initialized object.</returns>
        public static object BuildItem(IBuilder <WCSFBuilderStage> builder, IReadWriteLocator locator, object item)
        {
            Type   itemType    = item.GetType();
            string temporaryId = Guid.NewGuid().ToString();

            PolicyList policies = new PolicyList();

            policies.Set <ISingletonPolicy>(new SingletonPolicy(false), itemType, temporaryId);
            policies.Set <ICreationPolicy>(new DefaultCreationPolicy(), itemType, temporaryId);
            policies.Set <IPropertySetterPolicy>(new PropertySetterPolicy(), itemType, temporaryId);

            return(builder.BuildUp(locator, itemType, temporaryId, item, policies));
        }
Exemple #20
0
        public void CanClearAllPolicies()
        {
            PolicyList list = new PolicyList();

            list.Set <IBuilderPolicy>(new FakePolicy(), "1");
            list.Set <IBuilderPolicy>(new FakePolicy(), "2");

            Assert.IsNotNull(list.Get <IBuilderPolicy>("1"));
            Assert.IsNotNull(list.Get <IBuilderPolicy>("2"));

            list.ClearAll();

            Assert.IsNull(list.Get <IBuilderPolicy>("1"));
            Assert.IsNull(list.Get <IBuilderPolicy>("2"));
        }
        public void CanInjectStronglyTypedConstructor()
        {
            BuilderBase <BuilderStage> builder = new BuilderBase <BuilderStage>();

            builder.Strategies.AddNew <SingletonStrategy>(BuilderStage.PreCreation);
            builder.Strategies.AddNew <CreationStrategy>(BuilderStage.Creation);
            builder.Policies.SetDefault <ICreationPolicy>(new DefaultCreationPolicy());
            Locator locator = new Locator();

            IService service = new Service();
            IFoo     foo     = new Foo();

            locator.Add(new DependencyResolutionLocatorKey(typeof(IService), null), service);
            locator.Add(new DependencyResolutionLocatorKey(typeof(IFoo), null), foo);

            PolicyList policies = new PolicyList();

            policies.Set <ICreationPolicy>(new MockWithCtorDependenciesPolicy(), typeof(MockWithCtorDependencies), null);

            MockWithCtorDependencies mock = builder.BuildUp <MockWithCtorDependencies>(locator, null, null, policies);

            Assert.IsNotNull(mock);
            Assert.AreSame(service, mock.service);
            Assert.AreSame(foo, mock.foo);
        }
Exemple #22
0
        private IPolicyList GetPolicies()
        {
            PolicyList policies = new PolicyList();

            policies.Set <ILifetimePolicy>(new ContainerControlledLifetimeManager(), new NamedTypeBuildKey(typeof(object)));
            return(policies);
        }
Exemple #23
0
        public void ExceptionsThrownDuringBuildGetTranslated()
        {
            StrategyChain chain = new StrategyChain();

            chain.Add(new ThrowingStrategy("a"));

            PolicyList policies = new PolicyList();

            policies.Set <IShouldThrowPolicy>(new ShouldThrowPolicy(true, false, false, false), "a");

            Builder builder = new Builder();

            try
            {
                builder.BuildUp(null, null, policies, chain, typeof(object), null);
                Assert.Fail("Expected exception did not occur");
            }
            catch (BuildFailedException ex)
            {
                StringAssert.Contains(ex.BuildKey, typeof(object).Name);
                Assert.AreEqual(typeof(ThrowingStrategy).Name, ex.ExecutingStrategyTypeName);
                Assert.AreEqual(0, ex.ExecutingStrategyIndex);
                Assert.IsInstanceOfType(ex.InnerException, typeof(Exception));
            }
        }
Exemple #24
0
        /// <summary>
        /// Remove policies associated with building this type. This removes the
        /// compiled build plan so that it can be rebuilt with the new settings
        /// the next time this type is resolved.
        /// </summary>
        /// <param name="typeToInject">Type of object to clear the plan for.</param>
        /// <param name="name">Name the object is being registered with.</param>
        private void ClearExistingBuildPlan(Type typeToInject, string name)
        {
            var buildKey = new NamedTypeBuildKey(typeToInject, name);

            DependencyResolverTrackerPolicy.RemoveResolvers(policies, buildKey);
            policies.Set <IBuildPlanPolicy>(new OverriddenBuildPlanMarkerPolicy(), buildKey);
        }
        private IPolicyList GetPolicies()
        {
            PolicyList policies = new PolicyList();

            policies.Set <ILifetimePolicy>(new SingletonLifetimePolicy(), typeof(object));
            return(policies);
        }
Exemple #26
0
        private static PolicyList GetPolicies(IConfigurationSource configurationSource)
        {
            PolicyList policyList = new PolicyList();

            policyList.Set <IConfigurationObjectPolicy>(new ConfigurationObjectPolicy(configurationSource),
                                                        typeof(IConfigurationSource));
            return(policyList);
        }
Exemple #27
0
        public void CanAddPolicyToBagAndRetrieveIt()
        {
            PolicyList list = new PolicyList();

            list.Set<IBuilderPolicy>(new MockPolicy(), typeof(object), null);

            Assert.IsNotNull(list.Get<IBuilderPolicy>(typeof(object), null));
        }
        /// <summary>
        /// Builds a new instance of <paramref name="typeOfItem"/>.
        /// </summary>
        /// <param name="typeOfItem">The <see cref="Type"/> of the object to create and build.</param>
        /// <returns>The new and built object instance.</returns>
        public object BuildNewItem(Type typeOfItem)
        {
            string temporaryID = Guid.NewGuid().ToString();

            PolicyList policies = new PolicyList();

            policies.Set <ISingletonPolicy>(new SingletonPolicy(false), typeOfItem, temporaryID);
            policies.Set <ICreationPolicy>(new DefaultCreationPolicy(), typeOfItem, temporaryID);
            policies.Set <IPropertySetterPolicy>(new PropertySetterPolicy(), typeOfItem, temporaryID);

            return(_builder.BuildUp(
                       _locator,
                       typeOfItem,
                       temporaryID,
                       null,
                       policies));
        }
Exemple #29
0
        public void CanAddPolicyToBagAndRetrieveIt()
        {
            PolicyList list = new PolicyList();

            list.Set <IBuilderPolicy>(new MockPolicy(), typeof(object), null);

            Assert.IsNotNull(list.Get <IBuilderPolicy>(typeof(object), null));
        }
Exemple #30
0
 public void SetUp()
 {
     strategyChain = new StrategyChain();
     strategyChain.Add(new ConfiguredObjectStrategy());
     strategyChain.Add(new MockBuilderStrategy());
     policyList = new PolicyList();
     source     = new DictionaryConfigurationSource();
     policyList.Set <IConfigurationObjectPolicy>(new ConfigurationObjectPolicy(source), typeof(IConfigurationSource));
 }
Exemple #31
0
        /// <summary>
        /// Used when the creator of a root <see cref="WorkItem"/> needs to ensure that the WorkItem
        /// has properly built itself (so its dependencies get injected properly).
        /// </summary>
        protected internal void BuildUp()
        {
            // We use Guid.NewGuid() to generate a dummy ID, so that the WorkItem buildup sequence can
            // run (the WorkItem is already located with the null ID, which marks it as a service, so
            // the SingletonStrategy would short circuit and not do the build-up).

            Type   type        = GetType();
            string temporaryID = Guid.NewGuid().ToString();
            PropertySetterPolicy propPolicy = new PropertySetterPolicy();

            propPolicy.Properties.Add("Parent", new PropertySetterInfo("Parent", new ValueParameter(typeof(WorkItem), null)));

            PolicyList policies = new PolicyList();

            policies.Set <ISingletonPolicy>(new SingletonPolicy(false), type, temporaryID);
            policies.Set <IPropertySetterPolicy>(propPolicy, type, temporaryID);

            builder.BuildUp(locator, type, temporaryID, this, policies);
        }
		public void CanClearPolicy()
		{
			PolicyList list = new PolicyList();
			MockPolicy policy = new MockPolicy();

			list.Set<IBuilderPolicy>(policy, typeof(string), "1");
			list.Clear<IBuilderPolicy>(typeof(string), "1");

			Assert.IsNull(list.Get<IBuilderPolicy>(typeof(string), "1"));
		}
Exemple #33
0
        public void CanClearPolicy()
        {
            PolicyList list   = new PolicyList();
            FakePolicy policy = new FakePolicy();

            list.Set(typeof(string), string.Empty, typeof(IBuilderPolicy), policy);
            list.Clear(typeof(string), string.Empty, typeof(IBuilderPolicy));

            Assert.IsNull(list.GetOrDefault(typeof(IBuilderPolicy), typeof(string), out _));
        }
Exemple #34
0
        public void CanClearPolicy()
        {
            PolicyList list   = new PolicyList();
            FakePolicy policy = new FakePolicy();

            list.Set <IBuilderPolicy>(policy, typeof(string));
            list.Clear <IBuilderPolicy>(typeof(string));

            Assert.IsNull(list.Get <IBuilderPolicy>(typeof(string)));
        }
Exemple #35
0
        public void CanRegisterGenericPolicyAndRetrieveWithSpecificGenericInstance()
        {
            PolicyList list   = new PolicyList();
            FakePolicy policy = new FakePolicy();

            list.Set(policy, typeof(IDummy <>));

            FakePolicy result = list.Get <FakePolicy>(typeof(IDummy <int>));

            Assert.AreSame(policy, result);
        }
		public void TestSettingAndRetrievePolicy()
		{
			PolicyList policies = new PolicyList();
			MockCreationPolicy policy = new MockCreationPolicy();

			policies.Set<IBuilderPolicy>(policy, typeof (object), null);
			BuilderContext context = new BuilderContext(null, null, policies);

			IBuilderPolicy outPolicy = context.Policies.Get<IBuilderPolicy>(typeof (object), null);

			Assert.IsNotNull(outPolicy);
			Assert.AreSame(policy, outPolicy);
		}
		public void SettingPolicyViaContextDoesNotAffectPoliciesPassedToContextConstructor()
		{
			PolicyList policies = new PolicyList();
			MockCreationPolicy policy1 = new MockCreationPolicy();

			policies.Set<IBuilderPolicy>(policy1, typeof (object), null);
			BuilderContext context = new BuilderContext(null, null, policies);

			MockCreationPolicy policy2 = new MockCreationPolicy();
			context.Policies.Set<IBuilderPolicy>(policy2, typeof (string), null);

			Assert.AreEqual(1, policies.Count);
		}
Exemple #38
0
        public void BuilderCanTakeTransientPolicies()
        {
            Builder builder = new Builder();
            Locator locator = CreateLocator();
            PolicyList policies = new PolicyList();

            PropertySetterPolicy policy = new PropertySetterPolicy();
            policy.Properties.Add("IntProp", new PropertySetterInfo("IntProp", new ValueParameter<int>(96)));
            policies.Set<IPropertySetterPolicy>(policy, typeof(PropertyObject), null);

            PropertyObject obj = builder.BuildUp<PropertyObject>(locator, null, null, policies);

            Assert.AreEqual(96, obj.IntProp);
        }
Exemple #39
0
        public void TransientPoliciesOverrideBuilderPolicies()
        {
            Builder builder = new Builder();
            Locator locator = CreateLocator();
            PolicyList policies = new PolicyList();

            PropertySetterPolicy builderPolicy = new PropertySetterPolicy();
            builderPolicy.Properties.Add("IntProp", new PropertySetterInfo("IntProp", new ValueParameter<int>(11)));
            builder.Policies.Set<IPropertySetterPolicy>(builderPolicy, typeof(PropertyObject), null);

            PropertySetterPolicy transientPolicy = new PropertySetterPolicy();
            transientPolicy.Properties.Add("IntProp", new PropertySetterInfo("IntProp", new ValueParameter<int>(22)));
            policies.Set<IPropertySetterPolicy>(transientPolicy, typeof(PropertyObject), null);

            PropertyObject obj = builder.BuildUp<PropertyObject>(locator, null, null, policies);

            Assert.AreEqual(22, obj.IntProp);
        }
Exemple #40
0
        private void InitializeBuilderState()
        {
            registeredNames = new NamedTypesRegistry(ParentNameRegistry);
            extensions = new List<UnityContainerExtension>();

            lifetimeContainer = new LifetimeContainer();
            strategies = new StagedStrategyChain<UnityBuildStage>(ParentStrategies);
            buildPlanStrategies = new StagedStrategyChain<UnityBuildStage>(ParentBuildPlanStrategies);
            policies = new PolicyList(ParentPolicies);
            policies.Set<IRegisteredNamesPolicy>(new RegisteredNamesPolicy(registeredNames), null);

            cachedStrategies = null;
            cachedStrategiesLock = new object();
        }
 private IPolicyList GetPolicies()
 {
     PolicyList policies = new PolicyList();
     policies.Set<ILifetimePolicy>(new ContainerControlledLifetimeManager(), new NamedTypeBuildKey(typeof (object)));
     return policies;
 }
Exemple #42
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));
        }
        private static PolicyList GetPolicies(IConfigurationSource configurationSource)
        {
            PolicyList policyList = new PolicyList();
            policyList.Set<IConfigurationObjectPolicy>(new ConfigurationObjectPolicy(configurationSource), typeof(IConfigurationSource), null);

            return policyList;
        }