Castle Windsor:如何在我的配置中连接事件?

时间:2008-12-04 20:27:41

标签: .net xml events castle-windsor

我有这样的事情:

public interface IDeviceMonitor {
    int DeviceId { get; }
    event DeviceUpdatedHandler NewValueRecieved;
    void Start();
    void Stop();
}
public class DeviceInactivityDetector {
    ...
    public virtual void DeviceUpdated(IDeviceMonitor device, DeviceUpdatedArgs args) {
        ....
    }
}

目前,在我的应用程序启动文件中我做了

var dm = IoC.Container.Resolve<IDeviceMonitor>();
var did = IoC.Container.Resolve<DeviceInactivityDetector>();
dm.NewValueRecieved += new DeviceUpdatedHandler(did.DeviceUpdated);

我想我可以通过xml与我的城堡配置文件进行连接。但是如何?

1 个答案:

答案 0 :(得分:4)

使用EventWiring Facility,您可以使用配置将组件的方法(订阅者)连接到组件的事件(发布者)。

http://www.castleproject.org/container/facilities/v1rc3/eventwiring/index.html

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

<components>
    <component 
        id="DeviceInactivityDetector" 
        type=" . . ." />

    <component 
        id="IDeviceMonitor" 
        type=". . ." >
        <subscribers>
            <subscriber id="DeviceInactivityDetector" event="NewValueRecieved" handler="DeviceUpdated"/>
        </subscribers>
    </component>
</components>