Exemple #1
0
        public void OrderingAcrossStagesForStrategyChain()
        {
            StagedStrategyChain <BuilderStrategy, FakeStage> innerChain = new StagedStrategyChain <BuilderStrategy, FakeStage>();
            StagedStrategyChain <BuilderStrategy, FakeStage> outerChain = new StagedStrategyChain <BuilderStrategy, FakeStage>(innerChain);
            FakeStrategy innerStage1 = new FakeStrategy {
                Name = "innerStage1"
            };
            FakeStrategy innerStage2 = new FakeStrategy {
                Name = "innerStage2"
            };
            FakeStrategy outerStage1 = new FakeStrategy {
                Name = "outerStage1"
            };
            FakeStrategy outerStage2 = new FakeStrategy {
                Name = "outerStage2"
            };

            innerChain.Add(innerStage1, FakeStage.Stage1);
            innerChain.Add(innerStage2, FakeStage.Stage2);
            outerChain.Add(outerStage1, FakeStage.Stage1);
            outerChain.Add(outerStage2, FakeStage.Stage2);

            var chain = outerChain.ToArray();

            AssertOrder(chain, innerStage1, outerStage1, innerStage2, outerStage2);
        }
Exemple #2
0
        protected void InitializeRootContainer()
        {
            // Main strategy chain
            _strategies.Add(new BuildKeyMappingStrategy(), UnityBuildStage.TypeMapping);
            _strategies.Add(new LifetimeStrategy(), UnityBuildStage.Lifetime);
            _strategies.Add(new BuildPlanStrategy(), UnityBuildStage.Creation);

            // Build plan strategy chain
            _buildPlanStrategies.Add(new DynamicMethodConstructorStrategy(), BuilderStage.Creation);
            _buildPlanStrategies.Add(new DynamicMethodPropertySetterStrategy(), BuilderStage.Initialization);
            _buildPlanStrategies.Add(new DynamicMethodCallStrategy(), BuilderStage.Initialization);

            // Special Cases
            this[null, null] = _defaultPolicies;
            this[typeof(Func <>), string.Empty, typeof(ILifetimePolicy)]         = new PerResolveLifetimeManager();
            this[typeof(Func <>), string.Empty, typeof(IBuildPlanPolicy)]        = new DeferredResolveCreatorPolicy();
            this[typeof(Lazy <>), string.Empty, typeof(IBuildPlanCreatorPolicy)] = new GenericLazyBuildPlanCreatorPolicy();
            this[typeof(Array), string.Empty, typeof(IBuildPlanCreatorPolicy)]   =
                new DelegateBasedBuildPlanCreatorPolicy(typeof(UnityContainer).GetTypeInfo().GetDeclaredMethod(nameof(ResolveArray)),
                                                        context => context.OriginalBuildKey.Type.GetElementType());
            this[typeof(IEnumerable <>), string.Empty, typeof(IBuildPlanCreatorPolicy)] =
                new DelegateBasedBuildPlanCreatorPolicy(typeof(UnityContainer).GetTypeInfo().GetDeclaredMethod(nameof(ResolveEnumerable)),
                                                        context => context.BuildKey.Type.GetTypeInfo().GenericTypeArguments.First());
            // Register this instance
            RegisterInstance(typeof(IUnityContainer), null, this, new ContainerLifetimeManager());
        }
        protected virtual IStrategyChain CreateStrategyChain()
        {
            StagedStrategyChain <BuilderStage> strategies = new StagedStrategyChain <BuilderStage>();

            Strategies.ForEach(item => strategies.Add(CreateStrategy(item.Type), item.Stage));

            return(strategies.MakeStrategyChain());
        }
        public void OrderingAcrossStagesForStrategyChain()
        {
            StagedStrategyChain<FakeStage> innerChain = new StagedStrategyChain<FakeStage>();
            StagedStrategyChain<FakeStage> outerChain = new StagedStrategyChain<FakeStage>(innerChain);
            FakeStrategy innerStage1 = new FakeStrategy();
            FakeStrategy innerStage2 = new FakeStrategy();
            FakeStrategy outerStage1 = new FakeStrategy();
            FakeStrategy outerStage2 = new FakeStrategy();
            innerChain.Add(innerStage1, FakeStage.Stage1);
            innerChain.Add(innerStage2, FakeStage.Stage2);
            outerChain.Add(outerStage1, FakeStage.Stage1);
            outerChain.Add(outerStage2, FakeStage.Stage2);

            IStrategyChain chain = outerChain.MakeStrategyChain();

            AssertOrder(chain, innerStage1, outerStage1, innerStage2, outerStage2);
        }
        public void OrderingAcrossStagesForStrategyChain()
        {
            StagedStrategyChain <FakeStage> innerChain = new StagedStrategyChain <FakeStage>();
            StagedStrategyChain <FakeStage> outerChain = new StagedStrategyChain <FakeStage>(innerChain);
            FakeStrategy innerStage1 = new FakeStrategy();
            FakeStrategy innerStage2 = new FakeStrategy();
            FakeStrategy outerStage1 = new FakeStrategy();
            FakeStrategy outerStage2 = new FakeStrategy();

            innerChain.Add(innerStage1, FakeStage.Stage1);
            innerChain.Add(innerStage2, FakeStage.Stage2);
            outerChain.Add(outerStage1, FakeStage.Stage1);
            outerChain.Add(outerStage2, FakeStage.Stage2);

            IStrategyChain chain = outerChain.MakeStrategyChain();

            AssertOrder(chain, innerStage1, outerStage1, innerStage2, outerStage2);
        }
        public void InnerStrategiesComeBeforeOuterStrategiesInStrategyChain()
        {
            StagedStrategyChain<FakeStage> innerChain = new StagedStrategyChain<FakeStage>();
            StagedStrategyChain<FakeStage> outerChain = new StagedStrategyChain<FakeStage>(innerChain);
            FakeStrategy innerStrategy = new FakeStrategy();
            FakeStrategy outerStrategy = new FakeStrategy();
            innerChain.Add(innerStrategy, FakeStage.Stage1);
            outerChain.Add(outerStrategy, FakeStage.Stage1);

            IStrategyChain chain = outerChain.MakeStrategyChain();

            AssertOrder(chain, innerStrategy, outerStrategy);
        }
        public void InnerStrategiesComeBeforeOuterStrategiesInStrategyChain()
        {
            StagedStrategyChain <FakeStage> innerChain = new StagedStrategyChain <FakeStage>();
            StagedStrategyChain <FakeStage> outerChain = new StagedStrategyChain <FakeStage>(innerChain);
            FakeStrategy innerStrategy = new FakeStrategy();
            FakeStrategy outerStrategy = new FakeStrategy();

            innerChain.Add(innerStrategy, FakeStage.Stage1);
            outerChain.Add(outerStrategy, FakeStage.Stage1);

            IStrategyChain chain = outerChain.MakeStrategyChain();

            AssertOrder(chain, innerStrategy, outerStrategy);
        }
        public void MultipleChildContainers()
        {
            StagedStrategyChain<FakeStage> innerChain = new StagedStrategyChain<FakeStage>();
            StagedStrategyChain<FakeStage> outerChain = new StagedStrategyChain<FakeStage>(innerChain);
            StagedStrategyChain<FakeStage> superChain = new StagedStrategyChain<FakeStage>(outerChain);

            FakeStrategy innerStrategy = new FakeStrategy();
            FakeStrategy outerStrategy = new FakeStrategy();
            FakeStrategy superStrategy = new FakeStrategy();
            innerChain.Add(innerStrategy, FakeStage.Stage1);
            outerChain.Add(outerStrategy, FakeStage.Stage1);
            superChain.Add(superStrategy, FakeStage.Stage1);

            IStrategyChain chain = superChain.MakeStrategyChain();

            AssertOrder(chain, innerStrategy, outerStrategy, superStrategy);
        }
        public void MultipleChildContainers()
        {
            StagedStrategyChain <FakeStage> innerChain = new StagedStrategyChain <FakeStage>();
            StagedStrategyChain <FakeStage> outerChain = new StagedStrategyChain <FakeStage>(innerChain);
            StagedStrategyChain <FakeStage> superChain = new StagedStrategyChain <FakeStage>(outerChain);

            FakeStrategy innerStrategy = new FakeStrategy();
            FakeStrategy outerStrategy = new FakeStrategy();
            FakeStrategy superStrategy = new FakeStrategy();

            innerChain.Add(innerStrategy, FakeStage.Stage1);
            outerChain.Add(outerStrategy, FakeStage.Stage1);
            superChain.Add(superStrategy, FakeStage.Stage1);

            IStrategyChain chain = superChain.MakeStrategyChain();

            AssertOrder(chain, innerStrategy, outerStrategy, superStrategy);
        }
Exemple #10
0
        private static MockBuilderContext GetContext()
        {
            var chain = new StagedStrategyChain <IBuilderStrategy, BuilderStage>();

            chain.Add(new DynamicMethodConstructorStrategy(), BuilderStage.Creation);

            var policy = new DynamicMethodBuildPlanCreatorPolicy(chain);

            var context = new MockBuilderContext();

            context.Strategies.Add(new LifetimeStrategy());

            context.PersistentPolicies.SetDefault <IConstructorSelectorPolicy>(
                new ConstructorSelectorPolicy <InjectionConstructorAttribute>());
            context.PersistentPolicies.SetDefault <IBuildPlanCreatorPolicy>(policy);

            return(context);
        }
        private MockBuilderContext GetContext()
        {
            StagedStrategyChain <IBuilderStrategy, BuilderStage> chain = new StagedStrategyChain <IBuilderStrategy, BuilderStage>();

            chain.Add(new DynamicMethodPropertySetterStrategy(), BuilderStage.Initialization);

            DynamicMethodBuildPlanCreatorPolicy policy =
                new DynamicMethodBuildPlanCreatorPolicy(chain);

            MockBuilderContext context = new MockBuilderContext();

            context.Strategies.Add(new LifetimeStrategy());

            context.Policies.SetDefault <IConstructorSelectorPolicy>(
                new ConstructorSelectorPolicy <InjectionConstructorAttribute>());
            context.Policies.SetDefault <IPropertySelectorPolicy>(
                new PropertySelectorPolicy <DependencyAttribute>());
            context.Policies.SetDefault <IBuildPlanCreatorPolicy>(policy);

            return(context);
        }
Exemple #12
0
        public void MultipleChildContainers()
        {
            StagedStrategyChain <BuilderStrategy, FakeStage> innerChain = new StagedStrategyChain <BuilderStrategy, FakeStage>();
            StagedStrategyChain <BuilderStrategy, FakeStage> outerChain = new StagedStrategyChain <BuilderStrategy, FakeStage>(innerChain);
            StagedStrategyChain <BuilderStrategy, FakeStage> superChain = new StagedStrategyChain <BuilderStrategy, FakeStage>(outerChain);

            FakeStrategy innerStrategy = new FakeStrategy {
                Name = "innerStrategy"
            };
            FakeStrategy outerStrategy = new FakeStrategy {
                Name = "outerStrategy"
            };
            FakeStrategy superStrategy = new FakeStrategy {
                Name = "superStrategy"
            };

            innerChain.Add(innerStrategy, FakeStage.Stage1);
            outerChain.Add(outerStrategy, FakeStage.Stage1);
            superChain.Add(superStrategy, FakeStage.Stage1);

            var chain = superChain.ToArray();

            AssertOrder(chain, innerStrategy, outerStrategy, superStrategy);
        }
 public void RegisterStrategy(IBuilderStrategy strategy,
                              BuilderStage stage)
 {
     strategies.Add(strategy, stage);
 }