Created
October 27, 2011 09:22
-
-
Save Grinderofl/1319143 to your computer and use it in GitHub Desktop.
C# .NET: Ninject Dependency Resolver
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
// -------------------------------------------------------------------------------------------------------------------- | |
// <copyright file="NinjectDependencyResolver.cs" company=""> | |
// Nero Sule | |
// </copyright> | |
// <summary> | |
// Dependency Injection | |
// </summary> | |
// -------------------------------------------------------------------------------------------------------------------- | |
namespace Web.Infrastructure | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Web.Mvc; | |
using Ninject; | |
using Ninject.Syntax; | |
/// <summary> | |
/// Dependency Injection | |
/// </summary> | |
public class NinjectDependencyResolver : IDependencyResolver | |
{ | |
#region Constants and Fields | |
/// <summary> | |
/// The _kernel. | |
/// </summary> | |
private readonly IKernel _kernel; | |
#endregion | |
#region Constructors and Destructors | |
/// <summary> | |
/// Initializes a new instance of the <see cref="NinjectDependencyResolver"/> class. | |
/// </summary> | |
public NinjectDependencyResolver() | |
{ | |
this._kernel = new StandardKernel(); | |
this.AddBindings(); | |
} | |
#endregion | |
#region Properties | |
/// <summary> | |
/// Gets Kernel. | |
/// </summary> | |
public IKernel Kernel | |
{ | |
get | |
{ | |
return this._kernel; | |
} | |
} | |
#endregion | |
#region Public Methods | |
/// <summary> | |
/// The bind. | |
/// </summary> | |
/// <typeparam name="T"> | |
/// </typeparam> | |
/// <returns> | |
/// </returns> | |
public IBindingToSyntax<T> Bind<T>() | |
{ | |
return this._kernel.Bind<T>(); | |
} | |
#endregion | |
#region Implemented Interfaces | |
#region IDependencyResolver | |
/// <summary> | |
/// The get service. | |
/// </summary> | |
/// <param name="serviceType"> | |
/// The service type. | |
/// </param> | |
/// <returns> | |
/// The get service. | |
/// </returns> | |
public object GetService(Type serviceType) | |
{ | |
return this._kernel.TryGet(serviceType); | |
} | |
/// <summary> | |
/// The get services. | |
/// </summary> | |
/// <param name="serviceType"> | |
/// The service type. | |
/// </param> | |
/// <returns> | |
/// </returns> | |
public IEnumerable<object> GetServices(Type serviceType) | |
{ | |
return this._kernel.GetAll(serviceType); | |
} | |
#endregion | |
#endregion | |
#region Methods | |
/// <summary> | |
/// The add bindings. | |
/// </summary> | |
private void AddBindings() | |
{ | |
// Todo: Add bindings here | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment