internal override void DoBuildInstance(InjectionContext context, ParameterSet parameters, out T instance)
        {
            var matchingContext = context.ForceFindMatchingContext(_description);

            if (matchingContext != null)
            {
                if (!matchingContext.InstanceBuilt)
                {
                    // The current BuildOperation depends on a parent context that has the same ObjectDescription with itself,
                    // yet the parent context does not have a built instance either, that means a cyclic dependency problem.
                    throw CyclicDependencyException(new FakeInjectionContext(context, _description, parameters));
                }

                var myContext = matchingContext as InjectionContext <T>;
                if (myContext == null)
                {
                    throw new ImpossibleException();
                }
                instance = myContext.Instance;
            }
            else
            {
                instance = context.BuildInstance(_process, _description, parameters);
            }
        }
        internal override void DoBuildInstance(InjectionContext context, ParameterSet parameters, out T instance)
        {
            var matchingContext = context.FindMatchingContext(_description);

            if (matchingContext != null)
            {
                if (!matchingContext.InstanceBuilt)
                {
                    // The current BuildOperation depends on a parent context that has the same ObjectDescription with itself,
                    // yet the parent context does not have a built instance either, that means a cyclic dependency problem.
                    throw CyclicDependencyException(new FakeInjectionContext(context, _description, parameters));
                }

                var myContext = matchingContext as InjectionContext <T>;
                if (myContext == null)
                {
                    throw new ImpossibleException();
                }
                instance = myContext.Instance;

                // If we do find a matching context, then turn this into a shared one.
                InjectionOperatorHelper.UpgradeToSharedObjectBuilder(_builder, new SharedInjectionOperator <T>(_builder, _description, _lifetime, _process));
            }
            else
            {
                instance = context.BuildInstance(_process, _description, parameters);
            }
        }
        // A lock is only needed when the first time an instance is built
        internal override void DoBuildInstance(InjectionContext context, ParameterSet parameters, out T instance)
        {
            var injectionOperator = _builder.InjectionOperator;

            if (!ReferenceEquals(injectionOperator, this))
            {
                _builder.BuildInstance(context, parameters, out instance);
                return;
            }

            Monitor.Enter(_builder.ObjectRelation.SyncRoot);
            injectionOperator = _builder.InjectionOperator;
            if (!ReferenceEquals(injectionOperator, this))
            {
                Monitor.Exit(_builder.ObjectRelation.SyncRoot);
                _builder.BuildInstance(context, parameters, out instance);
                return;
            }

            try
            {
                var process = _configurationSet.CreateInjectionProcess <T>(context.Kernel);
                var oneoff  = new OneOffInjectionOperator <T>(_builder, _description, _lifetime, process);
                InjectionOperatorHelper.UpgradeToOneOffObjectBuilder(_builder, oneoff);
                oneoff.BuildInstance(context, parameters, out instance);
            }
            finally
            {
                Monitor.Exit(_builder.ObjectRelation.SyncRoot);
            }
        }
Exemple #4
0
        internal InjectionContext(InjectionContext context, ObjectDescription description, ParameterSet parameters)
        {
            _parentContext = context;
            _scope         = context.LifetimeScope;

            _description = description;
            _parameters  = parameters;
        }
        internal override void DoBuildInstance(InjectionContext context, ParameterSet parameters, out T instance)
        {
            var matchingContext = context.ForceFindMatchingContext(_description);

            if (matchingContext != null)
            {
                if (!matchingContext.InstanceBuilt)
                {
                    ////// Replace the temporary one with this, so we won't create an OneOffObjectBuilder any more the next time.
                    ////InjectionOperatorHelper.UpgradeToOneOffObjectBuilder(MyBuilder, this);
                    //if (context.InstanceBuilt)
                    //{
                    //    var newContext = new ViralSharedInjectionContext<T>(context, MyDescription, parameters);
                    //    _process.Execute(newContext);

                    //    var myContext = matchingContext as InjectionContext<T>;
                    //    if (myContext == null)
                    //        throw new ImpossibleException();
                    //    instance = myContext.Instance = newContext.Instance;
                    //}
                    //else

                    // The current BuildOperation depends on a parent context that has the same ObjectDescription with itself,
                    // yet the parent context does not have a built instance either, that means a cyclic dependency problem.
                    throw CyclicDependencyException(new FakeInjectionContext(context, _description, parameters));
                }
                else
                {
                    var myContext = matchingContext as InjectionContext <T>;
                    if (myContext == null)
                    {
                        throw new ImpossibleException();
                    }
                    instance = myContext.Instance;
                    // replace the InjectionOperator
                    InjectionOperatorHelper.UpgradeToSharedObjectBuilder(_builder, new SharedInjectionOperator <T>(_builder, _description, _lifetime, _process));
                }
            }
            else
            {
                // This ObjectBuilder has not built its first instance yet, so we must use a ViralSharedInjectionContext,
                // instead of a NonViralSharedInjectionContext, no matter what kind of InjectionContext the parent context is.
                instance = context.BuildAndShareInstance(_process, _description, parameters);
                // replace the InjectionOperator
                InjectionOperatorHelper.UpgradeToNonSharedObjectBuilder(_builder, new NonSharedInjectionOperator <T>(_builder, _description, _lifetime, _process));
            }
        }
Exemple #6
0
 internal ViralSharedInjectionContext(InjectionContext context, ObjectDescription description, ParameterSet parameters)
     : base(context, description, parameters)
 {
 }
Exemple #7
0
 internal FakeInjectionContext(InjectionContext context, ObjectDescription description, ParameterSet parameters)
     : base(context, description, parameters)
 {
 }
 internal override void BuildInstance(InjectionContext context, ParameterSet parameters, out T instance)
 {
     DoBuildInstance(context, parameters, out instance);
 }
        protected Exception CyclicDependencyException(InjectionContext currentContext)
        {
            var errMsg = ExceptionFormatter.Format(currentContext, Resources.CyclicDependencyFoundWhileBuildingInstanceForType, _description.ConcreteType.ToTypeName());

            return(new CyclicDependencyException(errMsg));
        }
 internal override void BuildInstance(InjectionContext context, ParameterSet parameters, out T instance)
 {
     instance = _lifetime.BuildInstance(context, this, parameters);
 }