Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ASP MVC 4 Dependency Injection Provider with Ninject
namespace Solution.IoC
{
public class DependencyInjectionProvider
{
private IKernel _kernel;
private void SetUpDependencyInjection()
{
//create _kernel
_kernel = new StandardKernel();
//bindings
_kernel.Bind<IJsDateHelper>().To<JsDateHelper>();
_kernel.Bind<IDbHelper>().To<DbHelper>();
_kernel.Bind<IUserRepository>().To<UserRepository>();
_kernel.Bind<ISiteRepository>().To<SiteRepository>();
_kernel.Bind<IClientDetailsRepository>().To<ClientDetailsRepository>();
_kernel.Bind<IFileDetailsRepository>().To<FileDetailsRepository>();
_kernel.Bind<IPublicationRepository>().To<PublicationRepository>();
}
public T Resolve<T>(System.Type service) {
return (T)_kernel.Get(service);
}
public void Dispose()
{
_kernel.Dispose();
}
public System.Web.Mvc.IDependencyResolver GetMvcDependencyResolver()
{
SetUpDependencyInjection();
return new MvcDependencyResolver(_kernel);
}
public System.Web.Http.Dependencies.IDependencyResolver GetHttpDependencyResolver()
{
SetUpDependencyInjection();
return new HttpDependencyResolver(_kernel);
}
}
}
namespace Solution.CMS
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
var dependencyInjectionProvider = new DependencyInjectionProvider();
// Set MVC depencendy resolver for asp mvc web project
DependencyResolver.SetResolver(dependencyInjectionProvider.GetMvcDependencyResolver());
// Set MVC depencendy resolver for asp mvc web API project
GlobalConfiguration.Configuration.DependencyResolver = dependencyInjectionProvider.GetHttpDependencyResolver();
// Remove the XM Formatter from the web api
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
public class HttpDependencyResolver : IDependencyResolver
{
private readonly IResolutionRoot _resolutionRoot;
public HttpDependencyResolver(IResolutionRoot kernel)
{
this._resolutionRoot = kernel;
}
public object GetService(Type serviceType)
{
return _resolutionRoot.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _resolutionRoot.GetAll(serviceType);
}
public IDependencyScope BeginScope()
{
// This example does not support child scopes, so we simply return 'this'.
return this;
}
public void Dispose()
{
// When BeginScope returns 'this', the Dispose method must be a no-op.
}
}
public class MvcDependencyResolver : IDependencyResolver
{
private readonly IResolutionRoot _resolutionRoot;
public MvcDependencyResolver(IResolutionRoot kernel)
{
this._resolutionRoot = kernel;
}
public object GetService(Type serviceType)
{
return _resolutionRoot.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _resolutionRoot.GetAll(serviceType);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment