We had a client recently upgrade to Sitecore 8 Update 4, who used both Ninject and Web Forms For Marketers. During the upgrade, we also migrated the Ninject code over to Simple Injector as the performance benefits have been proven. IoC Container Performance – The impact on Sitecore and IoC container benchmarks and features.
But after we did this, the Form Reports stopped reporting data. After some investigation it was clear that the data was being written to both the MongoDB and the SQL Reporting databases. On closer inspection on the reports form, the ajax calls being made to retrive the data were throwing errors.
FormsController has 2 constructors and out of the box Simple Injector does now allow that. One of the Simple Instructor desgin decisions is Allow only a single constructor. In most cases that is a great thing, all the code at Lightmaker follows that standard anyway. But this Controller is part of the WFFM code base, so we are limited in our options.
Looking at the 2 constructors we can see that one has an
IWFFMDataProvider injected in, but the default one populates that object from the config. It looks like that is the one we want to use. So how to get Simple Injector to ignore the 2nd constructor?
public class FormController : SitecoreController, IHasModelFactory, IHasFormDataManager
Fortunately Simple Injector can be extended to cope with this. We can use a custom
ConstructorResolutionBehavior class to “tell” Simple Injector which constructor to use.
Here is the resolution:
public class DefaultFirstConstructorBehavior : IConstructorResolutionBehavior
And set this when you create the container:
var container = new Container();
container.Options.ConstructorResolutionBehavior = new DefaultFirstConstructorBehavior();
Now, Simple Injector selects the default constructor for WFFM FormController actions and the reports are showing data again. I feel that we could make this a little safer by explicitly stating the controllers that get the default constructor. But for now in our project this works nicely. There hasn’t been any ill effects on any other part of the CMS environment yet!