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.
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.
The interface and a implementation could look like this
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.
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.