IoC-Castle Windsor-extends container 2.1

Source: Internet
Author: User
When a component is added to the Windsor container, MicroKernel first creates a ComponentModel object to describe the component information, and then uses a series of contributor for processing, including detecting the dependency between components and other information. For example, ConstructorDependenciesModelInspector collects public constructors and their dependent components and adds them to the ComponentModel object, while LifestyleModelInspector checks whether the component has implemented specific lifecycle control interfaces.

When a component is registered, use the IHandler interface for processing. If windsor can process the objects and components that the component depends on (for example, windsor already knows how to process all the constructor parameters, so that they can be used to construct the instance object of this component), the component becomes the Request status, indicating that the client code can request the instance object of this component through the container; otherwise, this component will be added to a list. After the related dependency is set, change it to the Request status.

Suppose we want to implement an IStartable facility by ourselves. When the object created by the windsor container belongs to the IStartable type, call the Start method of the interface immediately.
The namespace referenced in our test project is as follows:

using System;using System.Collections;using Castle.Core;using Castle.Core.Configuration;using Castle.MicroKernel;using Castle.MicroKernel.LifecycleConcerns;using Castle.MicroKernel.ModelBuilder;using Castle.Windsor;using Castle.Windsor.Configuration.Interpreters;

An Implementation of the IStartable interface and test is as follows:

public interface IStartable{    void Start();}public class MyTask : IStartable{    public void Start()    {        Console.WriteLine("I have been started~");    }}

Next, the implementation code of facility is as follows. For most of the processing, see the Annotations:

Public class StartableFacility: IFacility {// Save the component waiting to resolve the dependency, the StartableFacility is a singleton // Therefore, non-static Arrays can be used to keep all the waiting dependent components private ArrayList _ waitList = new ArrayList (); private IKernel _ kernel; // method public void Init (IKernel kernel, IConfiguration facilityConfig) {_ kernel = kernel; // Add a contributor, used to check whether the component implements the IStartable interface // if this interface is implemented, the contributor will be used to add the required processing _ kernel. componentModelBuilder. addContributor (new StartableInspector (); // This event handler _ kernel is called after each component is registered. componentRegistered + = new ComponentDataDelegate (OnComponentRegistered);} // method executed when Facility is terminated. In this example, public void Terminate () {} private void OnComponentRegistered (String key, IHandler handler) {bool startable = (bool) handler. componentModel. extendedProperties ["startable"]; if (startable) {// when the component registration is complete, if its status is HandlerState. waitingDependency // we add it to the waiting list. // This method is executed when every component registration is complete, therefore, if the dependent component has been registered //, we will set the component waiting for dependency to OK in the CheckWaitingList method. if (handler. currentState = HandlerState. waitingDependency) _ waitList. add (handler); else Start (key); // use windsor to request an instance object for this component} CheckWaitingList ();} private void Start (string key) {// MicroKernel creates an instance object of this component, object instance = _ kernel [key];} private void CheckWaitingList () {IHandler [] handlers = (IHandler []) _ waitList. toArray (typeof (IHandler); foreach (IHandler handler in handlers) {if (handler. currentState = HandlerState. valid) {Start (handler. componentModel. name); _ waitList. remove (handler) ;}}}// used to check whether the component implements the inspectorpublic class StartableInspector of the IStartable interface: IContributeComponentModelConstruction {public void ProcessModel (IKernel kernel, ComponentModel model) {bool startable = typeof (IStartable ). isAssignableFrom (model. implementation); model. extendedProperties ["startable"] = startable; if (startable) // if the IStartable interface is implemented, add a lifestyle step model. lifecycleSteps. add (LifecycleStepType. commission, new StartableConcern ();} private class StartableConcern: ILifecycleConcern {public void Apply (ComponentModel model, object component) {// here we implement IStartable interface processing, call its Start method (component as IStartable ). start ();}}}

The configuration example is as follows:

<configuration>    <configSections>        <section name="castle"            type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />    </configSections>    <castle>        <facilities>            <facility id="startable" type="Windsor.Test.StartableFacility, Windsor.Test" />        </facilities>        <components>            <component id="my.task"                service="Windsor.Test.IStartable, Windsor.Test"                type="Windsor.Test.MyTask, Windsor.Test" />        </components>    </castle></configuration>

In this way, we only need to execute
WindsorContainer container = new WindsorContainer (new XmlInterpreter ());
In this Code, the windsor container will create a MyTask Instance Object and call its Start method. An information is output in the Console window.

Reference: Introducing Castle-Part I

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.