Sitefinity Part 2 — Dependency injection in Sitefinity

This is the second part of the Sitefinity series. For your convenience you can find other parts in the table of contents in Sitefinity Part 1 — Capturing logs

In order to use DI in Sitefinity you need to configure two mechanisms. One for feather widgets and one for other stuff. Let’s begin with widgets.

Feather widgets

We would like to have dependencies injected to widgets’ controllers (using constructor). Sitefinity uses NInject to do that, so we need to inject our configuration into it.

First, we need to inject code into bootstrapping process. We add the following to the global.asax.cs:

Sitefinity will call this event during initialization. Now, we need to configure NInject:

Now our controllers will be created by the factory presented above.

Other services

If you need to create other stuff (e.g., membership provider) you need to inject dependencies to Unity container (yes, another DI container). First, modify global.asax.cs:

And now simply register all stuff:

That’s all.

Tested with Sitefinity version: 10.0.6411.0.