Home

Castle Stronghold

EventWiring Facility

The EventWiring Facility allows subscribers to connect to events exposed by publishers.

Facility Summary
Uses ProxyNo
Requires configurationYes
Uses attributeNo
Version1.0 RC 2
Maturity levelHigh

Introduction

With the EventWiring Facility you can use the configuration to connect component's methods (subscribers) to component's events (publishers).

Quick start

Just install the facility and add the proper configuration. You need to configure only subscribers.


<configuration>

  <facilities>

    <facility 
        id="event.wiring"
        type="Castle.Facilities.EventWiring.EventWiringFacility, Castle.MicroKernel" />

  </facilities>

</configuration>

Example

Consider the following classes:


public class SimplePublisher
{
    public event PublishEventHandler Event;

    public SimplePublisher()
    {
    }

    public void Trigger()
    {
        if (Event != null)
        {
            Event(this, new EventArgs()); 
        }
    }
}

public class SimpleListener
{
    public SimpleListener()
    {
    }

    public void OnPublishEvent(object sender, EventArgs e)
    {
        ...
    }
}

The class SimplePublisher exposes the event Event and we want to make the method OnPublishEvent on SimpleListener subscribe to this event. All we need to do is use the configuration:


<configuration>
    <facilities>
        <facility 
            id="event.wiring"
            type="Castle.Facilities.EventWiring.EventWiringFacility, Castle.MicroKernel" />
    </facilities>

    <components>
        <component 
            id="SimpleListener" 
            type="Castle.Facilities.EventWiring.Tests.Model.SimpleListener, Castle.Facilities.EventWiring.Tests" />

        <component 
            id="SimplePublisher" 
            type="Castle.Facilities.EventWiring.Tests.Model.SimplePublisher, Castle.Facilities.EventWiring.Tests" >
            <subscribers>
                <subscriber id="SimpleListener" event="Event" handler="OnPublish"/>
            </subscribers>
        </component>
    </components>
</configuration>
    
Warning

The Event Wiring facility won't support wiring in the subscribers (even for singleton components) side, only on the publishers.

In other words:

  • If your subscriber starts before your publisher it won't be wired.
  • If your subscriber is a transient one, only one (the first instantiated) will be wired.

The wiring will happen once the publisher component is created. We recommend that you use the Startable facility together with the EventWiring so you can start the publishers as soon as the components are ready.

Warning

The EventWiring facility must be registered/declared before the Startable facility. Otherwise, the order that the components are declared/registerd will be relevant.

Google
Search WWW Search castleproject.org