예제 #1
0
        /*----------------------------------------------------------------------------------------*/
        /// <summary>
        /// Activates an instance by executing the chain of activation strategies.
        /// </summary>
        /// <param name="context">The activation context.</param>
        public void Activate(IContext context)
        {
            Ensure.ArgumentNotNull(context, "context");
            Ensure.NotDisposed(this);

            if (context.Instance == null)
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Will create instance of type {0} for service {1}", Format.Type(context.Implementation), Format.Type(context.Service));
                }

                Strategies.ExecuteForChain(s => s.BeforeCreate(context));

                // Request a new instance from the binding's provider.
                context.Instance = context.Binding.Provider.Create(context);

                if (context.Instance == null)
                {
                    throw new ActivationException(ExceptionFormatter.ProviderCouldNotCreateInstance(context));
                }

                Strategies.ExecuteForChain(s => s.AfterCreate(context));

                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Instance of service {0} resolved successfully", Format.Type(context.Service));
                }
            }

            Strategies.ExecuteForChain(s => s.Initialize(context));
            Strategies.ExecuteForChain(s => s.AfterInitialize(context));
        }
예제 #2
0
        /*----------------------------------------------------------------------------------------*/
        /// <summary>
        /// Destroys an instance by executing the chain of destruction strategies.
        /// </summary>
        /// <param name="context">The context in which the instance was requested.</param>
        public void Destroy(IContext context)
        {
            Ensure.ArgumentNotNull(context, "context");
            Ensure.NotDisposed(this);

            if (context.Instance == null)
            {
                throw new InvalidOperationException(ExceptionFormatter.ContextDoesNotContainInstanceToRelease(context));
            }

            Strategies.ExecuteForChain(s => s.BeforeDestroy(context));
            Strategies.ExecuteForChain(s => s.Destroy(context));
            Strategies.ExecuteForChain(s => s.AfterDestroy(context));
        }
예제 #3
0
        /*----------------------------------------------------------------------------------------*/
        #region Public Methods
        /// <summary>
        /// Builds a new activation plan by inspecting the specified type.
        /// </summary>
        /// <param name="binding">The binding that was used to resolve the type being activated.</param>
        /// <param name="type">The type to examine.</param>
        /// <returns>An activation plan that will be used to build instances type.</returns>
        public IActivationPlan GetPlan(IBinding binding, Type type)
        {
            Ensure.ArgumentNotNull(binding, "binding");
            Ensure.ArgumentNotNull(type, "type");
            Ensure.NotDisposed(this);

            lock (Plans)
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Activation plan for type {0} requested by {1}",
                                 Format.Type(type), Format.Binding(binding));
                }

                if (Plans.ContainsKey(type))
                {
                    if (Logger.IsDebugEnabled)
                    {
                        Logger.Debug("Using already-generated plan from cache");
                    }

                    return(Plans[type]);
                }

                IActivationPlan plan = binding.Components.ActivationPlanFactory.Create(type);
                Plans.Add(type, plan);

                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Type has not been analyzed, building activation plan");
                }

                Strategies.ExecuteForChain(s => s.BeforeBuild(binding, type, plan));
                Strategies.ExecuteForChain(s => s.Build(binding, type, plan));
                Strategies.ExecuteForChain(s => s.AfterBuild(binding, type, plan));

                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Activation plan for {0} built successfully", Format.Type(type));
                }

                return(plan);
            }
        }
예제 #4
0
        /*----------------------------------------------------------------------------------------*/
        /// <summary>
        /// Releases the activation plan for the specified type, if one was created.
        /// </summary>
        /// <param name="binding">The binding which points to the type that should be released.</param>
        /// <param name="type">The type whose activation plan should be released.</param>
        public void ReleasePlan(IBinding binding, Type type)
        {
            Ensure.ArgumentNotNull(binding, "binding");
            Ensure.ArgumentNotNull(type, "type");
            Ensure.NotDisposed(this);

            lock (Plans)
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Releasing activation plan for type {0}", Format.Type(type));
                }

                if (!Plans.ContainsKey(type))
                {
                    if (Logger.IsDebugEnabled)
                    {
                        Logger.Debug("Activation plan for {0} has not been created or was already released, ignoring",
                                     Format.Type(type));
                    }

                    return;
                }

                IActivationPlan plan = Plans[type];

                Strategies.ExecuteForChain(s => s.BeforeRelease(binding, type, plan));
                Strategies.ExecuteForChain(s => s.Release(binding, type, plan));
                Strategies.ExecuteForChain(s => s.AfterRelease(binding, type, plan));

                Plans.Remove(type);

                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Finished releasing activation plan for type {0}", Format.Type(type));
                }
            }
        }