Monday, March 28, 2011

Presenter Discovery Strategy WebformsMVP

[Update: As per comment posted by Anonymous, Get at least the changeset from March 29th (http://webformsmvp.codeplex.com/SourceControl/changeset/changes/8d76d88f629e) for the following code to work]

Fearing the start of a flame war, I do like the WebformsMVP framework. It's opinionated and it's works well. That's not to say I don't like Asp MVC, I do but I don't believe it's the only weapon in the .Net web developers arsenal. That's like saying I should always use a hammer to drive in a nail, what happens if the nail weighs 5kg.... you need a sledgehammer. While that certainly is drawing an extremely long bow (Being a guy the true answer if you can use a big electric tool use it, if not use a 20 pound sledgehammer or gaffer tape anyhow...) You should use the correct tool for the right job, for instance if I require a rich internet application I will use MVC, otherwise I will use Webforms.In saying the above Webforms certainly can be a pretty rich client experience.Anyhow enough of the ranting and onto the real purpose of this blog post.Previous to version 1.0 of the WebformsMVP project the only way to override the default behaviour of presenter discovery was by using Attributes such as

[sourcecode language="csharp"]
[PresenterBinding(typeof(ProductsAdminPresenter))]
public partial class ProductsAdmin : MvpUserControl, IProductsAdminView
[/sourcecode]

With the release of version 1.0, if you don't like the default Presenter discovery strategy you can now roll your own because the Presenter discovery strategies are now pluggable.Let's say, for instance, that my Presenter classes are in the namespace MyProjectName.Core.Presenters. To allow for custom presenter discovery I can create a new class inheriting from the type ConventionBasedPresenterDiscoveryStrategy.
[sourcecode language="csharp"]
public partial class CustomPresenterDiscoveryStrategy: ConventionBasedPresenterDiscoveryStrategy
{
public override CandidatePresenterTypeFullNameFormats
{
get{new[]{"{namespace}.Core.Presenters.{presenter}"};}
}
}
[/sourcecode]
If the code is trying to resolve the presenter for the ProductsAdmin partial class the above code will tell the PresenterBinder to look for MyProjectName.Core.Presenters.ProductsAdminPresenter.To plug the custom implementation into the framework in the Application_Start method of the Global.asax file (or where ever you do Bootstrapping)
[sourcecode language="csharp"]
protected void Application_Start(object sender, EventArgs e)
{
PresenterBinder.DiscoveryStrategy = new CompositePresenterDiscoveryStrategy( new AttributeBasedPresenterDiscoveryStrategy(), new CustomPresenterDiscoveryStrategy());
}
[/sourcecode]

By including the AttributeBasedDiscoveryStrategy class you can still use attribute overrides for tasks such as using shared presenters, but it'll fall through to your custom strategy for everything else.

2 comments:

  1. Ah, I needed to see this. Thanks!

    ReplyDelete
  2. Also, before someone ends up pulling their hair out: there is a small problem with the 1.2 version. Get at least the changeset from March 29th (http://webformsmvp.codeplex.com/SourceControl/changeset/changes/8d76d88f629e) or Craig's article will look like bullocks to you. :)

    ReplyDelete