• русский (Россия)
    • English (United States)
    • العربية (مصر)
    • Deutsch (Deutschland)
    • Español (España, alfabetización internacional)
    • français (France)
    • हिंदी (भारत)
    • italiano (Italia)
    • 日本語 (日本)
    • 한국어 (대한민국)
    • Nederlands (Nederland)
    • polski (Polska)
    • ไทย (ไทย)
    • Türkçe (Türkiye)
    • Tiếng Việt (Việt Nam)
    • 中文(中华人民共和国)
    • 中文(香港特別行政區)
  • Вход
  • Зарегистрироваться

DotNetAge

Скрыть боковую панель

How to raise and consume events




This document describes how to define event and how to create a observer to consume event.

Define and raise event




  1. Inherit the EventBase abstract class and implement the ProcessEvent method.
  2. 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(IRepository respository;
{
_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




class_diagram

  • 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>



 


    Average:5
  • Читает
    (742)
  • (0)
  • Постоянная ссылка
Предыдущий:DotNetAge plugable infrastructure - Dependency injection
Доля в:

Комментарии (0)


  • rss
  • atom

Существует никаких комментариев, установленных в данной статье.
Valid XHTML 1.0!