Stefan Forsberg
Apr 15, 2010
(0 votes)

Easier dependency injection with EPiMVP

EPiMVP is an implementation of the WebForms MVP pattern created by Mattias Johansson and Joel Abrahamsson. While my general impression and opinion about WebForms MVP is quite similar to that Rob Conery expressed here, that’s in the context of choosing between MVC and WebForms. As I’m sure you’re all aware that’s not a choice we get to make when working with episerver (unless you’ve found a business model that let’s you develop gadgets full time in which case you can please contact me with a job offer =)).

Anyway, this post will be a short one that sort of does the same thing as I did in my previous post.


The presenter factory

MVP uses a presenter factory that’s responsible for delivering presenters, similar to the controller factory in MVC. Since you can create your own factory this makes for a good root to use for your dependency injection since it’s called every time a presenter is requested.

EPIMVP, much to my joy, comes with a StructureMapPresenterFactory so we don’t have to create one our self. (It looks to also come with a implementation for Ninject and if you’re into another IoC container it shouldn’t be too much of  a hassle to create your own factory.)

To use the factory we must inform MVP to use it which we’ll do in Application_Start of global.asax.

   1: protected void Application_Start(Object sender, EventArgs e)
   2: {
   3:     WebFormsMvp.Binder.PresenterBinder.Factory = new EPiMVP.StructureMap.StructureMapPresenterFactory();
   4:     XFormControl.ControlSetup += new EventHandler(XForm_ControlSetup);
   5: }


Injecting the dependencies

Injecting dependencies is now as simple as declaring them in the constructor of our presenters. Using the example Joel wrote about here we’ll add a dependency to our IFoo interface in the FlatterPresenter.

   1: protected readonly IFoo foo;
   3: public FlatterPresenter(IFlatterView view, 
   4:     FlatterPage pageData, IFoo foo)
   5:     : base(view, pageData)
   6: {
   7: = foo;
   8:     View.Load += View_Load;
   9:     View.FlatterRequested += View_FlatterRequested;
  10: }


The interface and a implementation could look like this

   1: public interface IFoo
   2: {
   3:     string SomeString();
   4: }
   6: public class Foo : IFoo
   7: {
   8:     public string SomeString()
   9:     {
  10:         return DateTime.Now.ToShortDateString();
  11:     }
  12: }


We’ll setup StructureMap to use the concrete class Foo whenever a type of IFoo is requested. This is also done in Application_Start in global.asax and the full method now looks like this.

   1: protected void Application_Start(Object sender, EventArgs e)
   2: {
   3:     ObjectFactory.Configure(x =>
   4:             {
   5:                 x.For<Presenters.IFoo>().Use<Presenters.Foo>();
   6:             }
   7:         );
   8:     WebFormsMvp.Binder.PresenterBinder.Factory = new EPiMVP.StructureMap.StructureMapPresenterFactory();
   9:     XFormControl.ControlSetup += new EventHandler(XForm_ControlSetup);
  10: }


Now we can use our IFoo implementation in our presenter class. It’s quite a nice change of pace compared to the Setter Injection / Build Up we have to do when using standard WebForms.

Apr 15, 2010


Please login to comment.
Latest blogs
Customizing Property Lists in Optimizely CMS

Generic property lists is a cool editorial feature that has gained a lot of popularity - in spite of still being unsupported (officially). But if y...

Allan Thraen | Oct 2, 2022 | Syndicated blog

Content Delivery API – The Case of the Duplicate API Refresh Token

Creating a custom refresh provider to resolve the issues with duplicate tokens in the DXC The post Content Delivery API – The Case of the Duplicate...

David Lewis | Sep 29, 2022 | Syndicated blog

New Optimizely certifications - register for beta testing before November 1st

In January 2023, Optimizely is making updates to the current versions of our certification exams to make sure that each exam covers the necessary...

Jamilia Buzurukova | Sep 28, 2022

Optimizely community meetup - Sept 29 (virtual + Melbourne)

Super excited to be presenting this Thursday the 29th of September at the Optimizely community meetup. For the full details and RSVP's see the...

Ynze | Sep 27, 2022 | Syndicated blog

Preview multiple Visitor Groups directly while browsing your Optimizely site

Visitor groups are great - it's an easy way to add personalization towards market segments to your site. But it does come with it's own set of...

Allan Thraen | Sep 26, 2022 | Syndicated blog

The Report Center is finally back in Optimizely CMS 12

With Episerver.CMS.UI 12.12.0 the Report Center is finally re-introduced in the core product.

Tomas Hensrud Gulla | Sep 26, 2022 | Syndicated blog