Exemplo n.º 1
0
        private object ResolveInternal(object scope)
        {
            var cachedInstance = this.Cache.TryGet(this);

            if (cachedInstance != null)
            {
                return(cachedInstance);
            }

            this.Request.ActiveBindings.Push(this.Binding);

            var reference = new InstanceReference {
                Instance = this.GetProvider().Create(this)
            };

            this.Request.ActiveBindings.Pop();

            if (reference.Instance == null)
            {
                if (!this.Kernel.Settings.AllowNullInjection)
                {
                    throw new ActivationException(ExceptionFormatter.ProviderReturnedNull(this));
                }

                if (this.Plan == null)
                {
                    this.Plan = this.Planner.GetPlan(this.Request.Service);
                }

                return(null);
            }

            if (scope != null)
            {
                this.Cache.Remember(this, reference);
            }

            if (this.Plan == null)
            {
                this.Plan = this.Planner.GetPlan(reference.Instance.GetType());
            }

            this.Pipeline.Activate(this, reference);

            return(reference.Instance);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Resolves the instance associated with this hook.
        /// </summary>
        /// <returns>The resolved instance.</returns>
        public object Resolve()
        {
            lock (Binding)
            {
                if (Request.ActiveBindings.Contains(Binding))
                    throw new ActivationException(ExceptionFormatter.CyclicalDependenciesDetected(this));

                var cachedInstance = Cache.TryGet(this);

                if (cachedInstance != null)
                    return cachedInstance;

                Request.ActiveBindings.Push(Binding);

                var reference = new InstanceReference { Instance = GetProvider().Create(this) };

                Request.ActiveBindings.Pop();

                if (reference.Instance == null)
                {
                    if (!this.Kernel.Settings.AllowNullInjection)
                    {
                        throw new ActivationException(ExceptionFormatter.ProviderReturnedNull(this));
                    }

                    if (this.Plan == null)
                    {
                        this.Plan = this.Planner.GetPlan(this.Request.Service);
                    }

                    return null;
                }

                if (GetScope() != null)
                    Cache.Remember(this, reference);

                if (Plan == null)
                    Plan = Planner.GetPlan(reference.Instance.GetType());

                Pipeline.Activate(this, reference);

                return reference.Instance;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Resolves the instance associated with this hook.
        /// </summary>
        /// <returns>The resolved instance.</returns>
        public object Resolve()
        {
            lock (Binding)
            {
                if (Request.ActiveBindings.Contains(Binding))
                {
                    throw new ActivationException(ExceptionFormatter.CyclicalDependenciesDetected(this));
                }

                Request.ActiveBindings.Push(Binding);

                var reference = new InstanceReference();

                reference.Instance = Cache.TryGet(this);

                if (reference.Instance != null)
                {
                    return(reference.Instance);
                }

                reference.Instance = GetProvider().Create(this);

                if (GetScope() != null)
                {
                    Cache.Remember(this, reference);
                }

                Request.ActiveBindings.Pop();

                if (Plan == null)
                {
                    Plan = Planner.GetPlan(reference.Instance.GetType());
                }

                Pipeline.Activate(this, reference);

                return(reference.Instance);
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Deactivates the instance in the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="reference">The instance reference.</param>
 public void Deactivate(IContext context, InstanceReference reference)
 {
     Ensure.ArgumentNotNull(context, "context");
     Strategies.Map(s => s.Deactivate(context, reference));
 }