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:

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:

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:

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:

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.