Presenter discovery

Tatham Oddie edited this page Nov 19, 2013 · 1 revision

Attributes

The presenter class can be explicitly mapped from the view by using the [PresenterBinding] attribute:

[PresenterBinding(typeof(SomePresenter))]
public partial class SomeView : MvpUserControl<SomeModel>
{
  //...
}

Convention

When the [PresenterBinding] Attribute is not found, the presenter type name will be deduced from the view's type name.

Web Forms MVP will try to look for the following, in the particular order:

  1. [view's namespace].[view type name]Presenter
  2. [assembly name containing the view].Logic.Presenters.[view type name]Presenter
  3. [assembly name containing the view].Logic.[view type name]Presenter
  4. [assembly name containing the view].[View type name]Presenter

Avoid naming the assembly which required special characters like dashes (-) which cannot be used in a namespace.

Roll your own

If you require to use a different discovery strategy to the default implementation you can plug your own implementation into the framework.

For a completely custom strategy, write a class that implements IPresenterDiscoveryStrategy.

If you just want to tune the convention, you can do this easily by inheriting from ConventionBasedPresenterDiscoveryStrategy.

For example:

public partial class CustomPresenterDiscoveryStrategy: ConventionBasedPresenterDiscoveryStrategy
{
  public override IEnumerable<string> CandidatePresenterTypeFullNameFormats
  {
    get
    {
      return new[] { "{namespace}.Core.Presenters.{presenter}" };
    };
  }
}

Register custom strategies in the Application_Start method of the Global.asax file:

protected void Application_Start(object sender, EventArgs e)
{
  PresenterBinder.DiscoveryStrategy = new CompositePresenterDiscoveryStrategy(
    new AttributeBasedPresenterDiscoveryStrategy(), 
    new CustomPresenterDiscoveryStrategy() 
  ); 
}

The CompositePresenterDiscoveryStrategy provides a fall-through approach. By including the AttributeBasedDiscoveryStrategy implementation 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.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.