WFFM and Simple Injector

Out of the box, Simple Injector allows1 to instantiating object of classes that have a single public constructor. Unfortunately, the Form Controller2 of Web Forms For Marketers3 has 2 such constructors.

I recommend the following post about Dependency Injection anti-pattern: multiple constructors. Unfortunately, in many cases, Sitecore Controllers are implemented with multiple constructors.

If you see the error below... welcome, you are trapped in the Sitecore dependency injection anti-pattern:

Error Rendering Controller: Sitecore.Forms.Mvc.Controllers.FormController, Sitecore.Forms.Mvc. Action: Index: No registration for type FormController could be found and an implicit registration could not be made. For the container to be able to create FormController it should have only one public constructor: it has 2.

Solution

With Simple Injector it is easy to change the default behavior, we have to override the Simple Injector default Constructor Resolution Behavior4 and write our own custom implementation of the IConstructorResolutionBehavior interface.

In this case, we just pick the constructor with the minimum ammount of parameters. If we don't have any constructors we fallback to the Simple Injector default behavior. This approach is working for our case, but be sure to select the right constructor, possibly you have to adapt this approach if you encounter a similar issue.

Now we need to tell Simple Injector that our implementation of the Constructor Resolution should be used.