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
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using Telerik.Sitefinity.Abstractions; using Telerik.Sitefinity.Data; namespace Cms { using System; public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped; } void Bootstrapper_Bootstrapped(object sender, EventArgs e) { NinjectControllerFactory.RegisterControllerFactory(); } } } |
Sitefinity will call this event during initialization. Now, we need to configure NInject:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
using System; using System.Web.Mvc; using System.Web.Routing; using Ninject; using Telerik.Microsoft.Practices.Unity; using Telerik.Sitefinity.Abstractions; using Telerik.Sitefinity.Frontend.Mvc.Infrastructure.Controllers; using Telerik.Sitefinity.Mvc; namespace Cms { public class NinjectControllerFactory : FrontendControllerFactory { private readonly IKernel _kernel = Telerik.Sitefinity.Frontend.FrontendModule.Current.DependencyResolver; protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { if (controllerType == null) { return null; } var controller = _kernel.Get(controllerType); return controller as IController; } private static void RegisterDependencies(IKernel dependencyResolver) { // Register your dependencies here: // dependencyResolver.Bind< IHaveASandwich >().To< QuarterPounderWithCheese>(); } public static void RegisterControllerFactory() { RegisterDependencies(Telerik.Sitefinity.Frontend.FrontendModule.Current.DependencyResolver); ObjectFactory.Container.RegisterType< ISitefinityControllerFactory, NinjectControllerFactory >(new ContainerControlledLifetimeManager()); var factory = ObjectFactory.Resolve< ISitefinityControllerFactory >(); ControllerBuilder.Current.SetControllerFactory(factory); } } } |
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
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using Telerik.Sitefinity.Abstractions; using Telerik.Sitefinity.Data; namespace Cms { using System; public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { UnityRegistrations.RegisterAllDependencies(); } } |
And now simply register all stuff:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using Telerik.Microsoft.Practices.Unity; using Telerik.Sitefinity.Abstractions; namespace Cms { public static class UnityRegistrations { public static void RegisterAllDependencies() { ObjectFactory.Initializing += (s, ev) => { RegisterDependencies(); }; } private static void RegisterDependencies() { // Here register your dependencies // ObjectFactory.Container.RegisterType< IHaveASandwich, QuarterPounderWithCheese >(); } } } |
That’s all.
Tested with Sitefinity version: 10.0.6411.0.