Add-ons with ASP.NET MVC, yes, it can be done - part 2!

A lot of people have been wondering if ASP.NET MVC will ever be popular among the web developers. One of the powerful things with ASP.NET Web Forms is that it easy to create or download an user control and add it to your own project. It would seem like that can't be done with ASP.NET MVC, and that will of course keep some developers away. Not all developers care about the beauty and cleaness of ASP.NET MVC, or not enough to switch anyway.

Let me just right away say this: It can be done! It might not be as easy as with Web Forms, and you might need to do more work yourself etc. But it can be done!

In what to put in an add-on framework - part 1 we talked about how to create add-ons for MVC Forum. So far we're stuck with an add-on without an UI, and that could be just what you needed, but I need an UI for my Akismet anti-spam add-on. Along with the UI we'll be creating a class that will hold the configuration of the add-on:

public class AkismetConfiguration : AsyncAddOnConfiguration<AkismetAddOn> {

	public AkismetConfiguration(IRepository<AddOnConfiguration> configRepo) : base(configRepo) { }

	public String Key {
		get { return this.GetString(Keys.AkismetKey); }
		set { this.Set(Keys.AkismetKey, value); }
	}

	// TODO: More properties! More code!
}

Again, we're working on an asynchronously add-on, so we're using the AsyncAddOnConfiguration as base instead of AddOnConfiguration (then we don't have to add our own Enabled and Delay properties).

Configuration storage done, let's create the controller for the UI:

public class AkismetConfigurationController : ForumAdminBaseController, IAntiSpamConfigurationController {

	public ViewResult Index() {
		return View(new AkismetViewModel { Key = this.config.Key, Enabled = this.config.Enabled, SpamScore = this.config.SpamScore, MarkAsSpamOnHit = this.config.MarkAsSpamOnHit, RunAsynchronously = this.config.RunAsynchronously });
	}

	[HttpPost]
	public ViewResult Index(AkismetViewModel model) {
		if (ModelState.IsValid) {
			return View(model);
		}
	}
}

So nothing tricky here. You inherit from the ForumAdminBaseController, this will make sure the user accessing the controller actually has access to the forum admin area. You have to implement an Index action, that way the Forum Admin knows how to link to your configuration controller. What you do from here is up to you, you control the controller and the views. So if you need several actions to handle the configuration of your add-on, you can have that.

One last thing about the add-on framework. If you need to configure your add-on with some default settings, you can create another class that implements the IInstallable interface. This class should implement an Install method, that method will be called when the basic installation is run.

public class AkismetInstall : IInstallable {
	private readonly IRepository<AddOnConfiguration> configRepo;

	public AkismetInstall(IRepository<AddOnConfiguration> configRepo) { this.configRepo = configRepo; }

	public void Install() {
		AkismetConfiguration conf = new AkismetConfiguration(this.configRepo);
		conf.Enabled = false;
		// TODO: More!
	}
}

Just remember that it all ties together in the dependency builder, so we need to tell MVC Forum how all the code we've written so far fit into the web application.

public class AkismetBuilder : IDependencyBuilder {
	public void Configure(IDIContainer container) {
		container.Register<IAntiSpamAddOn, AkismetAddOn>();
		container.Register<IAddOnConfiguration<AkismetAddOn>, AkismetConfiguration>();
		container.Register<IAntiSpamConfigurationController, AkismetConfigurationController>();
		// Event handlers/listener for new posts/topics!
		container.Register<IEventListener, AkismetAddOn>();
		container.Register<IInstallable, AkismetInstall>();
	}

	public void ValidateRequirements(IList<ApplicationRequirement> feedback) {}
}

That's about it, at least for the up-coming MVC Forum version 0.7 (Alpha 3).

Categories: MVC Forum - ASP.NET MVC - Alpha 3

Posted by Steen F. Tøttrup on 13 November 2011 17:27. There are 0 comments.

Write new comment