Introduction

When working on one on my recent projects, I needed to fire an event that contains a message. I 've looked for any EventArgs that contains a string property to be used as a message container, but I didn't find anything simple. So, I decided to create a set of EventArgs that contains properties I need for almost every project and put them in a separate library, so it can be reused. Then I got the idea, why not declaring the event itself and its firing method in that library to be able to fire the events from my project the same way I call a method. That's the idea behind Basic Events project, out of the box usage of events containing common properties.

Using the code 

In your class that you wish to fire the event

Declare private or protected attribute of Events class.

private BasicEvents.Events _events = new BasicEvents.Events();

Declare a public read only property to return the Events class instance that all the events will be fired from.

public BasicEvents.Events Events
{
get
{
return _events;
}
}   

Where you want to fire one of the basic events just call the related method:

_events.FireMessageReceived(this, "Demo");

Fire events methods:

  • FireMessageReceived: fires MessageReceived event.
  • FireTimeMessageReceived: fires TimeMessageReceived event.
  • FireExceptionReceived: fires ExceptionReceived event.

In your presentation tier, or the class where you intend to handle the events fired:

Provided that you make instance of the business tier library 

DMO_BLL.DMO _dmo = new DMO_BLL.DMO();

Register the event handlers

_dmo.Events.MessageReceived += new BasicEvents.Events.MessageReception(Events_MessageReceived);   

Create the Events_MessageReceived method

void Events_MessageReceived(object sender, BasicEvents.MessageEventArgs e)
{
Console.WriteLine(e.Message); 
}  

Run the business method in the business tier library

_dmo.DemoMethod();  

 Notes:

 The source code can be viewed under codeplex.com by clicking here

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架