-
-
Save LeeCheneler/3273ee1f259408d53d2980e5fb58d288 to your computer and use it in GitHub Desktop.
A small Library to configure Ninject (A Dependency Injection Library) with an ASP.NET Application.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Ninject; | |
using Ninject.Modules; | |
using Ninject.Web.Common; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using System.Web; | |
using System.Web.Mvc; | |
namespace Ninject.Mvc | |
{ | |
// A small Library to configure Ninject (A Dependency Injection Library) with a WebAPI Application. | |
// To configure, take the following steps. | |
// | |
// 1. Install Packages Ninject and Ninject.Web.Common | |
// 2. Remove NinjectWebCommon.cs in your App_Start Directory | |
// 3. Add this file to your project (preferrably in the App_Start Directory) | |
// 4. Add Your Bindings to the Load method of MainModule. | |
// You can add as many additional modules to keep things organized | |
// simply add them to the Modules property of the NinjectModules class | |
// 5. Add the following Line to your Global.asax | |
// NinjectContainer.RegisterModules(NinjectModules.Modules); | |
// 5b.To Automatically Register all NinjectModules in the Current Project, You should instead add | |
// NinjectContainer.RegisterAssembly() | |
// You are done. | |
/// <summary> | |
/// Resolves Dependencies Using Ninject | |
/// </summary> | |
public class NinjectResolver : IDependencyResolver | |
{ | |
public IKernel Kernel { get; private set; } | |
public NinjectResolver(params NinjectModule[] modules) | |
{ | |
Kernel = new StandardKernel(modules); | |
} | |
public NinjectResolver(Assembly assembly) | |
{ | |
Kernel = new StandardKernel(); | |
Kernel.Load(assembly); | |
} | |
public object GetService(Type serviceType) | |
{ | |
return Kernel.TryGet(serviceType); | |
} | |
public IEnumerable<object> GetServices(Type serviceType) | |
{ | |
return Kernel.GetAll(serviceType); | |
} | |
} | |
// List and Describe Necessary Modules | |
public class NinjectModules | |
{ | |
//Return Lists of Modules in the Application | |
public static NinjectModule[] Modules | |
{ | |
get | |
{ | |
//Return Modules you want to use for DI | |
return new[] { new MainModule() }; | |
} | |
} | |
//Main Module For Application. | |
public class MainModule : NinjectModule | |
{ | |
public override void Load() | |
{ | |
//TODO: Bind to Concrete Types Here | |
} | |
} | |
//You can create as many Modules as you wish | |
} | |
/// <summary> | |
/// Its job is to Register Ninject Modules and Resolve Dependencies Manually (If need be) | |
/// </summary> | |
public class NinjectContainer | |
{ | |
private static NinjectResolver _resolver; | |
//Register Ninject Modules | |
public static void RegisterModules(NinjectModule[] modules) | |
{ | |
_resolver = new NinjectResolver(modules); | |
DependencyResolver.SetResolver(_resolver); | |
} | |
public static void RegisterAssembly() | |
{ | |
_resolver = new NinjectResolver(Assembly.GetExecutingAssembly()); | |
//This is where the actual hookup to the MVC Pipeline is done. | |
DependencyResolver.SetResolver(_resolver); | |
} | |
//Manually Resolve Dependencies | |
public static T Resolve<T>() | |
{ | |
return _resolver.Kernel.Get<T>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment