Castle practice 2-startable facility

Source: Internet
Author: User

This section is for analysis.Facility,CastleIt can be injected, that is, for components, it may "infringe" the component itself. Below we provide an officialStartableOfFacilityStart.

Xianming white thisFacilityIs to achieve the purpose:Make an implementationCastle. model. istartableInterfaceProgramSelf-execution is possible when dependency is satisfied.

Public   Interface Istartable
{
Void Start (); // Run
Void Stop (); // Executed during destruction
}


First, let's take a look at this interface:Ilifecycleconcern, This interface has a method:

Public   Interface Ilifecycleconcern
{
Void Apply (componentmodel model, Object Component );
}


It is used to provide processing behavior in a specific life cycle of a component.

For example, as we mentioned in the previous sectionIinitializableThe container automatically calls the component initialization method.InitializeOnlyFacilityIs built-in (KernelBuilt-in ).

Next, we will use two classes to implement this interface:

FirstStartconcern, Processing: if it is implementedIstartableTo call itsStart ()Method:

Public   Void Apply (componentmodel model, Object Component)
{
(Component As Istartable). Start ();
}

SecondStopconcern, Processing: if the implementationIstartableTo call itsStop ()Method:

Public   Void Apply (componentmodel model, Object Component)
{
(Component As Istartable). Stop ();
}


Don't ask why, just know the function. We will continue to talk about it below.

 

Let's see how to implementFacility, Create a customFacilityCan be directly implementedIfacilityInterface, orAbstractfacilityAnd their different expenditures are onlyAbstractfacilityProvidedIfacilityThe default Implementation of the interface, you only need to implementInit ()Method. But in factIfacilityThere are not many things included, but there are only two:

Public   Interface Ifacility
{
// Facility is added to the container and executed immediately.
Void Init (ikernel kernel, iconfiguration facilityconfig );
// It is generally called in the dispose () of the container.
Void Terminate ();
}


Since we want to achieve the self-starting of the program, naturally, we should check its dependency when the component is added to the container and then start it. So how to make customFacilityWhat are the capabilities of processing components? See the followingCode, InFacilityWhen adding a container, two events are registered with the container:

Protected   Override   Void Init ()
{
// After the component is created
Kernel. componentmodelcreated + =   New Componentmodeldelegate (oncomponentmodelcreated );
// Triggered after component Registration
Kernel. componentregistered + =   New Componentdatadelegate (oncomponentregistered );
}

So the following thing happened like this:

FirstAddA component,ComponentregisteredThe event has occurred.

InOncomponentregisteredCheck the dependency. If the dependency is satisfied and the component implementsIstartableRequest to create this component.

Private   Void Start (string key)
{
Object Instance = Kernel [Key];
}

When creating a component,ComponentmodelcreatedCause, inComponentmodelcreatedAdd the lifecycle Processing Event in. Here we use the two implementations at the beginning of this article.IlifecycleconcernClass:StartconcernAndStopconcern.

SetStartconcernRegister as a componentLifecyclesteptype.Commission(Lifecycle) Periodic processing.

SetStopconcernRegister as a componentLifecyclesteptype.Decommission(End of Life) Periodic processing behavior.

After the component is created, enterLifecyclesteptype. CommissionCycle,StartconcernTriggered,Startconcern. Apply ()CallStart ()To achieve the purpose of self-starting.

When the component is removed from the container, the component entersLifecyclesteptype. decommission, ThenStopconcern. Apply ()Trigger componentStop ()In the end ".

StartableOfFacilityThe design principle has been completed, and the next step will be the practice.J

 

///   <Summary>
/// This is a class that implements istartable and mainly uses application. Run () to start a window.
/// Check his constructor.
///   </Summary>
Public   Class Applicationrunner: istartable
{
Private Form _ form;
Private Thread _ thread;

//This indicates that this class depends on the component of the form1 type.
PublicApplicationrunner (form1 form)
{
_ Form=Form;
}

# RegionIstartable Member

Public void Start ()
{< br> _ thread = New thread ( New threadstart (Startapp);
_ thread. start ();
}

Public VoidStop ()
{
MessageBox. Show ("Stop is called, but it do nothing.");
}

# Endregion

Private VoidStartapp ()
{
Application. Run (_ form );
}
}

WhereForm1Is a common window:

Public   Class Form1: system. Windows. Forms. Form
{
// ....
}


FinallyMain:

[Mtathread]
Static   Void Main ()
{
// Create a container
Iwindsorcontainer container =   New Windsorcontainer ( " ../Appconfig. xml " );

//Add facility
Container. addfacility ("Startable",NewStartablefacility ());

// Add an applicationrunner, at this time, the container detects that it is dependent on form1, so it does not start
container. addcomponent ( " appruner " , typeof (applicationrunner ));

//After form1 is added, applicationrunner starts to meet the dependency requirements.
Container. addcomponent ("Form1",Typeof(Form1 ));
}


OK ! This section is here. I think everyone knows the concept and usage of facility , do you feel that facility is powerful? Use the ## % * & component, bye ~ (What should I do next? Do you have any suggestions ?)

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.