/// <summary> /// Create a default <see cref="UnityContainer"/>. /// </summary> public UnityContainer() { // Root of the hierarchy _root = this; // Lifetime Container LifetimeContainer = new LifetimeContainer(this); // Registry InitializeRootRegistry(); ///////////////////////////////////////////////////////////// // Context _context = new ContainerContext(this); // Build Strategies _strategies = new StagedStrategyChain <BuilderStrategy, UnityBuildStage> { { new LifetimeStrategy(), UnityBuildStage.Lifetime }, // Lifetime { new BuildKeyMappingStrategy(), UnityBuildStage.TypeMapping }, // Mapping { new BuildPlanStrategy(), UnityBuildStage.Creation } // Build }; // Update on change _strategies.Invalidated += (s, e) => _strategiesChain = _strategies.ToArray(); _strategiesChain = _strategies.ToArray(); // Default Policies and Strategies SetDefaultPolicies(this); }
// Root Container Constructor internal ContainerContext(UnityContainer container, StagedStrategyChain <Pipeline, Stage> type, StagedStrategyChain <Pipeline, Stage> factory, StagedStrategyChain <Pipeline, Stage> instance) { // Container this context represents Container = container; // Lifetime Managers _typeLifetimeManager = TransientLifetimeManager.Instance; _factoryLifetimeManager = TransientLifetimeManager.Instance; _instanceLifetimeManager = new ContainerControlledLifetimeManager(); // Initialize Pipelines TypePipeline = type; TypePipeline.Invalidated += (s, e) => TypePipelineCache = TypePipeline.ToArray(); TypePipelineCache = TypePipeline.ToArray(); FactoryPipeline = factory; FactoryPipeline.Invalidated += (s, e) => FactoryPipelineCache = FactoryPipeline.ToArray(); FactoryPipelineCache = FactoryPipeline.ToArray(); InstancePipeline = instance; InstancePipeline.Invalidated += (s, e) => InstancePipelineCache = InstancePipeline.ToArray(); InstancePipelineCache = InstancePipeline.ToArray(); }
// Child Container Constructor internal ContainerContext(UnityContainer container) { // Container this context represents Container = container; var parent = Container._parent ?? throw new InvalidOperationException("Parent must not be null"); // Lifetime Managers _typeLifetimeManager = parent.Context._typeLifetimeManager; _factoryLifetimeManager = parent.Context._factoryLifetimeManager; _instanceLifetimeManager = parent.Context._instanceLifetimeManager; // TODO: Create on demand // Initialize Pipelines TypePipeline = new StagedStrategyChain <Pipeline, Stage>(parent.Context.TypePipeline); TypePipeline.Invalidated += (s, e) => TypePipelineCache = TypePipeline.ToArray(); TypePipelineCache = TypePipeline.ToArray(); FactoryPipeline = new StagedStrategyChain <Pipeline, Stage>(parent.Context.FactoryPipeline); FactoryPipeline.Invalidated += (s, e) => FactoryPipelineCache = FactoryPipeline.ToArray(); FactoryPipelineCache = FactoryPipeline.ToArray(); InstancePipeline = new StagedStrategyChain <Pipeline, Stage>(parent.Context.InstancePipeline); InstancePipeline.Invalidated += (s, e) => InstancePipelineCache = InstancePipeline.ToArray(); InstancePipelineCache = InstancePipeline.ToArray(); }
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); }
/// <summary> /// Create a <see cref="Unity.UnityContainer"/> with the given parent container. /// </summary> /// <param name="parent">The parent <see cref="Unity.UnityContainer"/>. The current object /// will apply its own settings first, and then check the parent for additional ones.</param> private UnityContainer(UnityContainer parent) { // Parent _parent = parent ?? throw new ArgumentNullException(nameof(parent)); _parent.LifetimeContainer.Add(this); // Root of the hierarchy _root = _parent._root; // Lifetime LifetimeContainer = new LifetimeContainer(this); // Registry Register = InitAndAdd; ///////////////////////////////////////////////////////////// SetDefaultPolicies = parent.SetDefaultPolicies; // Context and policies _context = new ContainerContext(this); // Strategies _strategies = _parent._strategies; _strategiesChain = _parent._strategiesChain; _strategies.Invalidated += (s, e) => _strategiesChain = _strategies.ToArray(); // Caches SetDefaultPolicies(this); }
/// <summary> /// Create a default <see cref="UnityContainer"/>. /// </summary> public UnityContainer() { _root = this; // WithLifetime LifetimeContainer = new LifetimeContainer(this); // Registrations _registrations = new Registrations(ContainerInitialCapacity); // Context _context = new ContainerContext(this); // Methods _get = Get; _getGenericRegistration = GetOrAddGeneric; _isExplicitlyRegistered = IsExplicitlyRegisteredLocally; IsTypeExplicitlyRegistered = IsTypeTypeExplicitlyRegisteredLocally; GetRegistration = GetOrAdd; Register = AddOrUpdate; GetPolicy = Get; SetPolicy = Set; ClearPolicy = Clear; // Build Strategies _strategies = new StagedStrategyChain <BuilderStrategy, UnityBuildStage> { { // Array new ArrayResolveStrategy( typeof(UnityContainer).GetTypeInfo().GetDeclaredMethod(nameof(ResolveArray)), typeof(UnityContainer).GetTypeInfo().GetDeclaredMethod(nameof(ResolveGenericArray))), UnityBuildStage.Enumerable }, { // Enumerable new EnumerableResolveStrategy( typeof(UnityContainer).GetTypeInfo().GetDeclaredMethod(nameof(ResolveEnumerable)), typeof(UnityContainer).GetTypeInfo().GetDeclaredMethod(nameof(ResolveGenericEnumerable))), UnityBuildStage.Enumerable }, { new BuildKeyMappingStrategy(), UnityBuildStage.TypeMapping }, // Mapping { new LifetimeStrategy(), UnityBuildStage.Lifetime }, // WithLifetime { new BuildPlanStrategy(), UnityBuildStage.Creation } // Build }; // Update on change _strategies.Invalidated += OnStrategiesChanged; _strategiesChain = _strategies.ToArray(); // Default Policies and Strategies SetDefaultPolicies(this); Set(typeof(Func <>), All, typeof(LifetimeManager), new PerResolveLifetimeManager()); Set(typeof(Func <>), All, typeof(ResolveDelegateFactory), (ResolveDelegateFactory)DeferredFuncResolverFactory.DeferredResolveDelegateFactory); Set(typeof(Lazy <>), All, typeof(ResolveDelegateFactory), (ResolveDelegateFactory)GenericLazyResolverFactory.GetResolver); // Register this instance ((IUnityContainer)this).RegisterInstance(typeof(IUnityContainer), null, this, new ContainerLifetimeManager()); }
public void InnerStrategiesComeBeforeOuterStrategiesInStrategyChain() { StagedStrategyChain <BuilderStrategy, FakeStage> innerChain = new StagedStrategyChain <BuilderStrategy, FakeStage>(); StagedStrategyChain <BuilderStrategy, FakeStage> outerChain = new StagedStrategyChain <BuilderStrategy, FakeStage>(innerChain); FakeStrategy innerStrategy = new FakeStrategy(); FakeStrategy outerStrategy = new FakeStrategy(); innerChain.Add(innerStrategy, FakeStage.Stage1); outerChain.Add(outerStrategy, FakeStage.Stage1); var chain = outerChain.ToArray(); AssertOrder(chain, innerStrategy, outerStrategy); }
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); }