What to put in an add-on framework - part 1?
Initially when I started out on the MVC Forum project, I wasn't planning on introducing an add-on framework already in the first full release, but when I started working on anti-spam features and search indexing, another 2 feature that wasn't planned for now, I figured I might as well have a go at making it possible for others to plug add-ons into MVC forum.
Everything in this post describes how MVC Forum works right now, and how it will work in the next alpha release of MVC Forum (version 0.7). So it's important to note that we do appriciate people trying to use MVC Forum and maybe even develop add-ons for it, but we will be making breaking changes in the next releases. This might break the third party add-ons, or maybe not - end-of-warning. For now it's possible to create add-ons that can act as event handlers, and can plug into the Forum Admin area, on the Anti-Spam and Search pages.
So how much code do it require to create an add-on for MVC Forum?
The example I'll use is the Akismet add-on that can be found in the mvcForum.AddOns assembly. First off you'll need to create a class that contains the actual code for checking the content against the Akismet web-service. This class is an anti-spam add-on, so it should have the IAntiSpamAddOn interface, and because I'll implement the event handling code (MVC Forum specific) in the same class, it will also have the IEventListener<TEvent> interface. Out of the box, MVC Forum throws these four events when content is created/changed: NewPostEvent, NewTopicEvent, PostUpdatedEvent, TopicUpdatedEvent. Because we want to be able to run the event handling asynchronously, I'll use the IAsyncEventListener<TEvent> interface instead of the IEventListener<TEvent> interface.
public class AkismetAddOn : IAntiSpamAddOn,
IAsyncEventListener<NewPostEvent>,
IAsyncEventListener<NewTopicEvent>,
IAsyncEventListener<PostUpdatedEvent>,
IAsyncEventListener<TopicUpdatedEvent> {
public void Handle(Object payload) { /* TODO: Code */ }
public void Queue(NewTopicEvent payload) { /* TODO: Code */ }
public void Queue(NewPostEvent payload) { /* TODO: Code */ }
public void Queue(TopicUpdatedEvent payload) { /* TODO: Code */ }
public void Queue(PostUpdatedEvent payload) { /* TODO: Code */ }
public void Handle(TopicUpdatedEvent payload) { /* TODO: Code */ }
public void Handle(PostUpdatedEvent payload) { /* TODO: Code */ }
public void Handle(NewPostEvent payload) { /* TODO: Code */ }
public void Handle(NewTopicEvent payload) { /* TODO: Code */ }
public Boolean RunAsynchronously {
get {
return true;
}
}
public Byte Priority {
get {
return (Byte)EventPriority.High;
}
}
}
If you've looked at the code on Codeplex, you might have noticed the dependency builders, and to get our new add-on into MVC Forum at run-time, we'll add another class, a dependency builder that will wire everything together.
public class AkismetBuilder : IDependencyBuilder {
public void Configure(IDIContainer container) {
// Event handlers/listener for new posts/topics!
container.Register<IEventListener, AkismetAddOn>();
}
public void ValidateRequirements(IList<ApplicationRequirement> feedback) {}
}
Now our code will get hit whenever one of the four events are triggered, and we can check the content for spam. Great!
In part 2 we'll take a look at how to add a configuration for our add-on, and let the users change the configuration through the Forum Admin area.
Categories: MVC Forum - Alpha 3 - Akismet - Add-ons
Posted by Steen F. Tøttrup on 08 November 2011 17:52. There are 0 comments.
Write new comment
