spring.net sample -> ApplicationContext and IEventRegistry

来源:互联网 发布:js获取data属性 编辑:程序博客网 时间:2024/06/07 19:36

16.4. ApplicationContext and IEventRegistry

译文:NcFire(牛超)

16.4.1. Introduction

The example program Spring.Examples.EventRegistry shows how to use the application context to wire .NET events in a loosely coupled manner.

Spring.Examples.EventRegistry示例程序展示了如果在松散连结的方式中使用应用程序上下文来组线.NET事件的。

Loosely coupled eventing is normally associated with Message Oriented Middleware (MOM) where a daemon process acts as a message broker between other independent processes. Processes communicate indirectly with each other by sending messages though the message broker. The process that initiates the communication is known as a publisher and the process that receives the message is known as the subscriber. By using an API specific to the middleware these processes register themselves as either publishers or subscribers with the message broker. The communication between the publisher and subscriber is considered loosley coupled because neither the publisher or subscriber has a direct reference to each other, the messages brokers acts as an intermediary between the two processes. The IEventRegistry is the analogue of the message broker as applied to .NET events. Publishers are classes that invoke a .NET event, subscribers are the classes that register interest in these events, and the messages sent between them are instances of System.EventArgs. The implementation of IEventRegistry determine the exact semantics of the notification style and coupling between subscribers and publishers.

松散连结事件通常与面向消息中间件(MOM)联合使用,该中间件在另外两个独立的加工间通过一个消息截获来进行后台处理。加工之间的通讯不靠直接发送消息而借助于消息截取。发起通讯的加工被称为发布者而接收消息的是订阅者。通过对中间件使用一个具体的API,这些加工便会通过消息截取来注册为发布者或者订阅者。发布者与订阅者之间的通讯被认为是松散连结的因为二者彼此没有直接的引用,消息截取则在两个加工中扮演了媒介者的角色。IEventRegistry接口应用于.NET事件与消息截取功能是相似的。发布者是调用.NET事件的类,订阅者则注册表示是对这些事件感兴趣的类。它们之间的消息传递则是System.EventArgs的实例。IEventRegistry接口的实现决定了通知样式的准确语义以及在发布者与订阅者间连结它们。

The IApplicationContext interface extends the IEventRegistry interface and implementations of IApplicationContext delegate the event registry functionality to an instance of Spring.Objects.Events.Support.EventRegistry. IEventRegistry is a simple inteface with one publish method and two subscribe methods. Refer to Section 3.14.4, “Loosely coupled events” for a reminder of their signatures. The Spring.Objects.Events.Support.EventRegistry implementation is essentially a convenience to decouple the event wiring process between publisher and subscribers. In this implementation, after the event wiring is finished, publishers are directly coupled to the subscribers via the standard .NET eventing mechanisms. Alternate implementations could increase the decoupling futher by having the event registry subscribe to the events and be responsible for then notifying the subscribers.

IApplicationContext接口扩展了接口IEventRegistryIApplicationContext的实现为Spring.Objects.Events.Support.EventRegistry. IEventRegistry的一个实例委托代理了事件注册功能性,IEventRegistry是个提供了一个发布方法与两个订阅方法的简单接口。查阅章节3.14.4“松散连结事件”以获得它们的用法说明。Spring.Objects.Events.Support.EventRegistry实现在本质上对于减弱发布者与订阅者间的事件配线加工是很方便的。

In this example the class MyClientEventArgs is a subclass of System.EventArgs that defines a string property EventMessage. The class MyEventPublisher defines a public event with the delgate signature void SimpleClientEvent( object sender, MyClientEventArgs args ) The method void ClientMethodThatTriggersEvent1() fires this event. On the subscribing side, the class MyEventSubscriber contains a method, HandleClientEvents that matches the delegate signature and has a boolean property which is set to true if this method is called.

在本例中类System.EventArgs定义了一个string类型的属性EventMessage,而类MyClientEventArgsSystem.EventArgs的子类。类MyEventPublisher定义了一个带有delegate标签的void SimpleClientEvent( object sender, MyClientEventArgs args )的公共事件。方法void ClientMethodThatTriggersEvent1()引发了这个事件。在订阅方,类MyEventSubscriber包含了一个HandleClientEvents方法,它匹配了这个代理标签,这个类也包含了boolean型的属性以记录当HandleClientEvents被调用时该属性被设置为true

The publisher and subscriber classes are defined in an application context configuration file but that is not required in order to participate with the event registry. The main program, EventRegistryApp creates the application context and asks it for an instance of MyEventPublisher The publisher is registered with the event registry via the call, ctx.PublishEvents( publisher ). The event registry keeps a reference to this publisher for later use to register any subscribers that match its event signature. Two subscribers are then created and one of them is wired to the publisher by calling the method ctx.Subscribe( subscriber, typeof(MyEventPublisher) ) Specifying the type indicates that the subscriber should be registered only to events from objects of the type MyEventPublisher. This acts as a simple filtering mechanism on the subscriber.

发布者与订阅者类是在应用程序上下文配置文件中被定义的,但为了参与事件注册那就不是必要的啦。主程序EventRegistryApp创建了这个应用程序上下文并且从中获取了MyEventPubliser的一个实例。发布者通过调用ctx.PublishEvents(publisher)被加入到事件注册中。这个事件注册保持了这个发布者的引用,这是为了后续注册一些匹配这个事件标签的订阅者。

The publisher then fires the event using normal .NET eventing semantics and the subscriber is called. The subscriber prints a message to the console and sets a state variable to indicate it has been called. The program then simply prints the state variable of the two subscribers, showing that only one of them (the one that registered with the event registry) was called.

这个发布者接着使用普通的.NET事件语义来引发事件并且调用订阅者。订阅者向控制台打印一个消息并且设置一个状态变量以暗示它已经被调用。这个程序会简单的打印两个订阅者的状态变量,展示了它们中只有一个(被加入到事件注册中的那个)会被调用。