Dashboard > MicroKernel/Windsor > Home > Startable Facility
Log In   View a printable version of the current page.
Startable Facility
Added by Krzysztof Kozmic, last edited by Krzysztof Kozmic on Jan 22, 2010  (view change)
Labels: 
(None)


Introduction

Startable facility allows objects to be "Started" after they are created, and/or to be "Stopped" once they are released.

Following code samples assume that the kernel with registered facility is in scope

using Castle.Facilities.Startable;
IKernel container = new DefaultKernel();
container.AddFacility<StartableFacility>();

Assuming we have a startable component, we register the component, then resolve it, do some work with it, and then resolve it, printing out each step to console...

container.Register(Component.For<Startable>());
Console.WriteLine("Registered!");

var component = container.Resolve<Startable>();
Console.WriteLine("Resoled!");

component.DoWork();

container.ReleaseComponent(component);
Console.WriteLine("Released!");

If we execute this code, the following will be printed out:

Created!
Started!
Registered!
Resoled!
Workdone!
Released!

Notice that component was instantiated and started during its registration.

Making components startable

There are couple of ways of making the component startable

IStartable

Facility registers as startable all components implementing IStartable interface

public interface IStartable
{
    void Start();
    void Stop();
}

When you implement it, you can register the component just like any other usual component (like in the example above) and it will just work.

POCO approach

Startable components do not need to implement IStartable. You can make almost any component startable, with explicit registration

container.Register(
    Component.For<PocoComponent>()
        .StartUsingMethod("Start")
        .StopUsingMethod("Stop")
    );

You can skip either Start or Stop method if you don't need it.

Stop and Start methods have to be public, have return type of 'void' and zero parameters.

 

Strongly typed POCO (trunk only)

If you're using trunk version (post v2.1) you can also use strongly typed version of the above code:

 container.Register(
    Component.For<PocoComponent>()
        .StartUsingMethod(c => c.Start)
        .StopUsingMethod(c => c.Stop)
    );

XML configuration

As most facilities, you can also configure Startable Facility via XML config file

If you don't register the facility in code, you can register it in config file:

<facility id="startable"
    type="Castle.Facilities.Startable.StartableFacility, Castle.MicroKernel" / >

And register your startable components using additional startable attributes:

<component id="mycomponent"
    type="Namespace.MyComponent, Assembly"
    startable="true" startMethod="StartListener" stopMethod="StopListener" / >

You need to specify startable="true" and optionally either startMethod, stopMethod or both

External resources

Blog post by Mike Hadlow about the facility (Jan 16, 2010)
Blog post by Alex Henderson about the facility (Apr 29, 2007) - written long ago but still accurate

Site running on a free Atlassian Confluence Community License granted to Castle Project. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.4 Build:#809 Jun 12, 2007) - Bug/feature request - Contact Administrators