/// <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)
        {
            // Validate input
            Debug.Assert(null != parent, nameof(parent));
            Debug.Assert(null != parent._root, nameof(parent._root));

            // Register with parent
            _parent = parent;
            _root   = parent._root;
            _parent.LifetimeContainer.Add(this);

            // Defaults and policies
            LifetimeContainer = new LifetimeContainer(this);
            ModeFlags         = parent._root.ModeFlags;
            Defaults          = _root.Defaults;
            Context           = new ContainerContext(this);

            // Dynamic Members
            Register = InitAndAdd;

            // Validators
            ValidateType              = _root.ValidateType;
            ValidateTypes             = _root.ValidateTypes;
            DependencyResolvePipeline = _root.DependencyResolvePipeline;
        }
Beispiel #2
0
        /// <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)
        {
            // Validate input
            Debug.Assert(null != parent);
            Debug.Assert(null != parent._root);

            // Register with parent
            _parent = parent;
            _root   = parent._root;
            _parent.LifetimeContainer.Add(this);

            // Defaults and policies
            LifetimeContainer = new LifetimeContainer(this);
            ExecutionMode     = parent._root.ExecutionMode;
            Defaults          = _root.Defaults;
            Context           = new ContainerContext(this);

            // Dynamic Members
            Register = InitAndAdd;

            // Validators
            ValidateType              = _root.ValidateType;
            ValidateTypes             = _root.ValidateTypes;
            DependencyResolvePipeline = _root.DependencyResolvePipeline;


            /////////////////////////////////////////////////////////////
            // Build Mode

            var build = _root.ExecutionMode.BuildMode();

            PipelineFromRegistration = build switch
            {
                ModeFlags.Activated => PipelineFromRegistrationActivated,
                ModeFlags.Compiled => PipelineFromRegistrationCompiled,
                _ => (FromRegistration)PipelineFromRegistrationOptimized
            };

            PipelineFromUnregisteredType = build switch
            {
                ModeFlags.Activated => PipelineFromUnregisteredTypeActivated,
                ModeFlags.Compiled => PipelineFromUnregisteredTypeCompiled,
                _ => (FromUnregistered)PipelineFromUnregisteredTypeOptimized
            };

            PipelineFromOpenGeneric = build switch
            {
                ModeFlags.Activated => PipelineFromOpenGenericActivated,
                ModeFlags.Compiled => PipelineFromOpenGenericCompiled,
                _ => (FromOpenGeneric)PipelineFromOpenGenericOptimized
            };
        }
Beispiel #3
0
 public ConstructorDiagnostic(DefaultPolicies defaults, UnityContainer container)
     : base(defaults, container)
 {
 }
 public FieldProcessor(DefaultPolicies defaults)
     : base(defaults)
 {
 }
Beispiel #5
0
        /// <summary>
        /// Create a default <see cref="UnityContainer"/>.
        /// </summary>
        public UnityContainer(ModeFlags mode = ModeFlags.Optimized)
        {
            if (!mode.IsValid())
            {
                throw new ArgumentException("'Activated' and 'Diagnostic' flags are mutually exclusive.");
            }

            /////////////////////////////////////////////////////////////
            // Initialize Root
            _root             = this;
            ExecutionMode     = mode;
            LifetimeContainer = new LifetimeContainer(this);
            Register          = AddOrReplace;

            //Built-In Registrations

            // Defaults
            Defaults = new DefaultPolicies(this);

            // IUnityContainer, IUnityContainerAsync
            var container = new ExplicitRegistration(this, null, typeof(UnityContainer), new ContainerLifetimeManager())
            {
                Pipeline = (ref PipelineContext c) => c.Container
            };

            Debug.Assert(null != container.LifetimeManager);

            // Create Registries
            _metadata = new Metadata();
            _registry = new Registry(Defaults);
            _registry.Set(typeof(IUnityContainer), null, container);       // TODO: Remove redundancy
            _registry.Set(typeof(IUnityContainerAsync), null, container);
            _registry.Set(typeof(IUnityContainer), null, container.LifetimeManager.Pipeline);
            _registry.Set(typeof(IUnityContainerAsync), null, container.LifetimeManager.Pipeline);

            /////////////////////////////////////////////////////////////
            // Built-In Features

            var func = new PolicySet(this);

            _registry.Set(typeof(Func <>), func);
            func.Set(typeof(LifetimeManager), new PerResolveLifetimeManager());
            func.Set(typeof(TypeFactoryDelegate), FuncResolver.Factory);                                                              // Func<> Factory
            _registry.Set(typeof(Lazy <>), new PolicySet(this, typeof(TypeFactoryDelegate), LazyResolver.Factory));                   // Lazy<>
            _registry.Set(typeof(IEnumerable <>), new PolicySet(this, typeof(TypeFactoryDelegate), EnumerableResolver.Factory));      // Enumerable
            _registry.Set(typeof(IRegex <>), new PolicySet(this, typeof(TypeFactoryDelegate), RegExResolver.Factory));                // Regular Expression Enumerable


            /////////////////////////////////////////////////////////////
            // Pipelines

            var typeFactory = new TypeFactoryPipeline();
            var lifetime    = new LifetimePipeline();

            // Mode of operation
            if (ExecutionMode.IsDiagnostic())
            {
                /////////////////////////////////////////////////////////////
                // Setup Diagnostic mode

                var diagnostic = new DiagnosticPipeline();

                // Create Context
                Context = new ContainerContext(this,
                                               new StagedStrategyChain <Pipeline, Stage> // Type Build Pipeline
                {
                    { diagnostic, Stage.Diagnostic },
                    { typeFactory, Stage.Factory },
                    { new MappingDiagnostic(), Stage.TypeMapping },
                    { new ConstructorDiagnostic(this), Stage.Creation },
                    { new FieldDiagnostic(this), Stage.Fields },
                    { new PropertyDiagnostic(this), Stage.Properties },
                    { new MethodDiagnostic(this), Stage.Methods }
                },
                                               new StagedStrategyChain <Pipeline, Stage> // Factory Resolve Pipeline
                {
                    { diagnostic, Stage.Diagnostic },
                    { new FactoryPipeline(), Stage.Factory }
                },
                                               new StagedStrategyChain <Pipeline, Stage> // Instance Resolve Pipeline
                {
                    { diagnostic, Stage.Diagnostic },
                    { typeFactory, Stage.Factory }
                });

                // Build process
                DependencyResolvePipeline = ValidatingDependencyResolvePipeline;

                // Validators
                ValidateType       = DiagnosticValidateType;
                ValidateTypes      = DiagnosticValidateTypes;
                CreateErrorMessage = CreateDiagnosticMessage;
            }
            else
            {
                // Create Context
                Context = new ContainerContext(this,
                                               new StagedStrategyChain <Pipeline, Stage> // Type Build Pipeline
                {
                    { lifetime, Stage.Lifetime },
                    { typeFactory, Stage.Factory },
                    { new MappingPipeline(), Stage.TypeMapping },
                    { new ConstructorPipeline(this), Stage.Creation },
                    { new FieldPipeline(this), Stage.Fields },
                    { new PropertyPipeline(this), Stage.Properties },
                    { new MethodPipeline(this), Stage.Methods }
                },
                                               new StagedStrategyChain <Pipeline, Stage> // Factory Resolve Pipeline
                {
                    { lifetime, Stage.Lifetime },
                    { new FactoryPipeline(), Stage.Factory }
                },
                                               new StagedStrategyChain <Pipeline, Stage> // Instance Resolve Pipeline
                {
                    { typeFactory, Stage.Factory }
                });
            }


            /////////////////////////////////////////////////////////////
            // Build Mode

            var build = _root.ExecutionMode.BuildMode();

            PipelineFromRegistration = build switch
            {
                ModeFlags.Activated => PipelineFromRegistrationActivated,
                ModeFlags.Compiled => PipelineFromRegistrationCompiled,
                _ => (FromRegistration)PipelineFromRegistrationOptimized
            };

            PipelineFromUnregisteredType = build switch
            {
                ModeFlags.Activated => PipelineFromUnregisteredTypeActivated,
                ModeFlags.Compiled => PipelineFromUnregisteredTypeCompiled,
                _ => (FromUnregistered)PipelineFromUnregisteredTypeOptimized
            };

            PipelineFromOpenGeneric = build switch
            {
                ModeFlags.Activated => PipelineFromOpenGenericActivated,
                ModeFlags.Compiled => PipelineFromOpenGenericCompiled,
                _ => (FromOpenGeneric)PipelineFromOpenGenericOptimized
            };
        }
Beispiel #6
0
 public FieldDiagnostic(DefaultPolicies defaults) : base(defaults)
 {
 }
Beispiel #7
0
        /// <summary>
        /// Create a default <see cref="UnityContainer"/>.
        /// </summary>
        public UnityContainer(ModeFlags mode = ModeFlags.Optimized)
        {
            if (!mode.IsValid())
            {
                throw new ArgumentException("'Activated' and 'Diagnostic' flags are mutually exclusive.");
            }

            /////////////////////////////////////////////////////////////
            // Initialize Root
            _root = this;

            // Defaults and policies
            ModeFlags         = mode;
            Defaults          = new DefaultPolicies(this);
            LifetimeContainer = new LifetimeContainer(this);
            Register          = AddOrReplace;

            // Create Registry and set Factory strategy
            _metadata = new Registry <int[]>();
            _registry = new Registry <IPolicySet>(Defaults);


            /////////////////////////////////////////////////////////////
            //Built-In Registrations

            // IUnityContainer, IUnityContainerAsync
            var container = new ImplicitRegistration(this, null)
            {
                Pipeline = (ref BuilderContext c) => c.Async ? (object)Task.FromResult <object>(c.Container) : c.Container
            };

            _registry.Set(typeof(IUnityContainer), null, container);
            _registry.Set(typeof(IUnityContainerAsync), null, container);

            // Built-In Features
            var func = new PolicySet(this);

            _registry.Set(typeof(Func <>), func);
            func.Set(typeof(LifetimeManager), new PerResolveLifetimeManager());
            func.Set(typeof(TypeFactoryDelegate), FuncResolver.Factory);                                                         // Func<> Factory
            _registry.Set(typeof(Lazy <>), new PolicySet(this, typeof(TypeFactoryDelegate), LazyResolver.Factory));              // Lazy<>
            _registry.Set(typeof(IEnumerable <>), new PolicySet(this, typeof(TypeFactoryDelegate), EnumerableResolver.Factory)); // Enumerable
            _registry.Set(typeof(IRegex <>), new PolicySet(this, typeof(TypeFactoryDelegate), RegExResolver.Factory));           // Regular Expression Enumerable


            /////////////////////////////////////////////////////////////
            // Pipelines

            var factory  = new FactoryPipeline();
            var lifetime = new LifetimePipeline();

            // Mode of operation
            if (ModeFlags.IsOptimized())
            {
                /////////////////////////////////////////////////////////////
                // Setup Optimized mode


                // Create Context
                Context = new ContainerContext(this,
                                               new StagedStrategyChain <Pipeline, Stage> // Type Build Pipeline
                {
                    { lifetime, Stage.Lifetime },
                    { factory, Stage.Factory },
                    { new MappingPipeline(), Stage.TypeMapping },
                    { new ConstructorPipeline(this), Stage.Creation },
                    { new FieldPipeline(this), Stage.Fields },
                    { new PropertyPipeline(this), Stage.Properties },
                    { new MethodPipeline(this), Stage.Methods }
                },
                                               new StagedStrategyChain <Pipeline, Stage> // Factory Resolve Pipeline
                {
                    { lifetime, Stage.Lifetime },
                    { factory, Stage.Factory }
                },
                                               new StagedStrategyChain <Pipeline, Stage> // Instance Resolve Pipeline
                {
                    { lifetime, Stage.Lifetime },
                });
            }
            else
            {
                /////////////////////////////////////////////////////////////
                // Setup Diagnostic mode

                var diagnostic = new DiagnosticPipeline();

                // Create Context
                Context = new ContainerContext(this,
                                               new StagedStrategyChain <Pipeline, Stage> // Type Build Pipeline
                {
                    { diagnostic, Stage.Diagnostic },
                    { lifetime, Stage.Lifetime },
                    { factory, Stage.Factory },
                    { new MappingDiagnostic(), Stage.TypeMapping },
                    { new ConstructorDiagnostic(this), Stage.Creation },
                    { new FieldDiagnostic(this), Stage.Fields },
                    { new PropertyDiagnostic(this), Stage.Properties },
                    { new MethodDiagnostic(this), Stage.Methods }
                },
                                               new StagedStrategyChain <Pipeline, Stage> // Factory Resolve Pipeline
                {
                    { diagnostic, Stage.Diagnostic },
                    { lifetime, Stage.Lifetime },
                    { factory, Stage.Factory }
                },
                                               new StagedStrategyChain <Pipeline, Stage> // Instance Resolve Pipeline
                {
                    { diagnostic, Stage.Diagnostic },
                    { lifetime, Stage.Lifetime },
                });

                // Build process
                DependencyResolvePipeline = ValidatingDependencyResolvePipeline;

                // Validators
                ValidateType  = DiagnosticValidateType;
                ValidateTypes = DiagnosticValidateTypes;
                CreateMessage = CreateDiagnosticMessage;
            }
        }
Beispiel #8
0
 public MethodProcessor(DefaultPolicies defaults, UnityContainer container)
     : base(defaults, typeof(InjectionMethodAttribute), container)
 {
 }
Beispiel #9
0
 protected ParametersProcessor(DefaultPolicies defaults, Type attribute, UnityContainer container)
     : base(defaults)
 {
     Container = container;
     Markers   = new[] { attribute };
 }
Beispiel #10
0
 public ConstructorProcessor(DefaultPolicies defaults, UnityContainer container)
     : base(defaults, typeof(InjectionConstructorAttribute), container)
 {
     SelectMethod = SmartSelector;
 }
Beispiel #11
0
 public PropertyProcessor(DefaultPolicies defaults)
     : base(defaults)
 {
 }
 public PropertyDiagnostic(DefaultPolicies defaults)
     : base(defaults)
 {
 }
Beispiel #13
0
 public MethodDiagnostic(DefaultPolicies defaults, UnityContainer container)
     : base(defaults, container)
 {
 }