Event application, providing extended functions for the System

Source: Internet
Author: User
Tags blogengine
At the beginning of the system design, you may need to reserve an extended interface for the system so that you can add functions to the system after or after system development. blogengine. the extension mechanism in the. NET system is actually the application of events.
Nature of the extension mechanism:
Define a static event member in a persistent class and subscribe to the event to implement the extended function.

Based on the comments posted by users in the blogengine. net system, the system sends email notifications to describe the blogengine. Net extension mechanism.
The first step is to extend the interface, that is, to define the event:
Public class post
{
//.........
Public static event eventhandler <eventargs> commentadded;
}

Then, define a method to trigger an event to notify the subscribed event object that the event has occurred.
Protected virtual void oncommentadded (Comment comment)
{
If (commentadded! = NULL)
{
Commentadded (comment, new eventargs ());
}
}
Next, call this method after the comment is successfully added.
Public void addcomment (Comment comment)
{
Canceleventargs E = new canceleventargs ();
Onaddingcomment (comment, e );
If (! E. Cancel)
{
Comments. Add (comment );
Dataupdate ();
Oncommentadded (comment); // here
Sendconfigurications (comment );
}
}

So far, only a custom event has been implemented. :-) here is a key point, that is, the event Member, which is static.
Let's talk about the implementation of the extended functions.

[Extension ("sends an e-mail to the blog owner whenever a comment is added", "1.3", "blogengine. net ")] // This attribute is used to tell the extension manager about the description, version, and author of the current extension.
Public class sendcommentmail
{
Public sendcommentmail ()
{
Post. commentadded + = new eventhandler <eventargs> (post_commentadded); // subscribe to events
}

Private void post_commentadded (Object sender, eventargs E)
{
//... Specific emailCode.
}
}

The following is also a key point: To apply the extended functions to the system.

Void application_start (Object sender, eventargs E)
{
Try
{
// Mono does not use "_ code" as the name of the app_code assembly.
// Rather, it uses "app_code", which is case sensitive!
String assemblyname = "_ code ";
Type T = type. GetType ("Mono. RunTime ");
If (T! = NULL)
Assemblyname = "app_code ";

Assembly A = assembly. Load (assemblyname );
Type [] types = A. gettypes ();

Foreach (type in types)
{
Object [] attributes = type. getcustomattributes (typeof (extensionattribute), false );
Foreach (Object attribute in attributes)
{
If (extensionmanager. extensionenabled (type. Name ))
{
A. createinstance (type. fullname );
}
}
}
}
Catch (system. Io. filenotfoundexception)
{
// There are no extensions in the app_code folder, this error can be safely ignored
}
}

This type of extension is easy to understand. Of course, it may do less .:-)


Blogengine. NET is an open source blog system, address: http://www.codeplex.com/blogengine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.