This document describes how to define event and how to create a observer to consume event.
Define and raise event
- Inherit the EventBase abstract class and implement the ProcessEvent method.
- Create the event instance and call "Raise" method to rasie an event.
The following table list the properties for EventBase class:
Property | Type | Description |
---|---|---|
HttpContext | HttpContextBase | Pass the current http context object to observers. |
Name | string | Set the event name. |
The following table list the Raise method overloads for EventBase class:
Name | Description |
---|---|
Raise() | Raise an event |
Raise(HttpContextBase httpContext) | Raise an event and pass the current http context to observers. |
The following example define a OrderEvent object and raised in Purchase Action.
public class Order
{
public int ID{get;set;}
...
}
//Define OrderEvent class
public class OrderEvent:EventBase
{
public OrderEvent(Order order)
{
this.Order=order;
if (order.ID>0)
Name="NewOrder";
else
Name="EditOrder";
}
public Order Order{get;private set;}
}
public class OrderSampleController:Controller
{
IRepository_respository;
public OrderSampleController(IRepositoryrespository;
{
_respository=respository;
}
[HttpPost]
public ActionResult Purchase(Order order)
{
//Save order
respository.Create(order);
respository.Submit();
//Raise the "NewOrder" event.
new OrderEvent(order).Raise(HttpContext);
return View();
}
}
Creating observer
- EventObserver -The observer base class implement IObserver
,IDisposable interfaces and provides ProcessEvent(EventBase e) method to handling events. - EventNamebaseObserver - Defines an event name base observer class, this observer execute when the event name is match.
- EventTypebaseObserver
- Defines an event type base observer class, this observer execute when the event type is match. - TemplatedMailEventObserver - Defines a observer can send templated email.
The following example shows how to create an oberver to handle the OrderEvent
public class NewOrderObserver : EventNamebaseObserver
{
public void Process(EventBase e)
{
var _event=e as OrderEvent;
//Handling new order event.
_event.Order.State=New;
...
}
}
public class EditOrderObserver : EventNamebaseObserver
{
public void Process(EventBase e)
{
var _event=e as OrderEvent;
//Handing edit order event.
_event.Order.LastUpdate=DateTime.Now;
...
}
}
Register oberser
Register the observers to unity.config and set the EventName property value to observer.
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="NewOrderObserver" type="Example.NewOrderObserver,Example" />
<alias alias="EditOrderObserver" type="Example.NewOrderObserver,Example" />
<register type="EventObserver" mapTo="NewOrderObserver" name="newOrderObserver">
<property name="EventName" value="NewOrder" />
register>
<register type="EventObserver" mapTo="EditOrderObserver" name="editOrderObserver">
<property name="EventName" value="EditOrder" />
register>
unity>
-
-
Читает(742)
-
Trackback(0)
-
Постоянная ссылка
Комментарии (0)