コード例 #1
0
        internal override void DoBuildInstance(ISharingLifetimeScope scope, ParameterSet parameters, out T instance)
        {
            var context = new ViralSharedInjectionContext <T>(scope, _description, parameters);

            _process.Execute(context);
            instance = context.Instance;
            // replace the ObjectBuilder
            InjectionOperatorHelper.UpgradeToNonSharedObjectBuilder
                (_builder, new NonSharedInjectionOperator <T>(_builder, _description, _lifetime, _process));
        }
コード例 #2
0
        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));
            }
        }