Lidarr/NzbDrone.Web/Ninject.Web.Mvc/NinjectControllerFactory.cs

88 lines
2.7 KiB
C#
Raw Normal View History

2010-10-12 02:49:27 +00:00
//
// Authors: Nate Kohari <nate@enkari.com>, Josh Close <narshe@gmail.com>
// Copyright (c) 2007-2009, Enkari, Ltd.
//
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
// See the file LICENSE.txt for details.
//
namespace Ninject.Web.Mvc
{
2010-10-18 18:04:07 +00:00
using System;
using System.Web.Mvc;
using System.Web.Routing;
2010-10-12 02:49:27 +00:00
/// <summary>
2010-10-18 18:04:07 +00:00
/// A controller factory that creates <see cref="IController"/>s via Ninject.
/// </summary>
public class NinjectControllerFactory : DefaultControllerFactory
{
/// <summary>
/// Gets the kernel that will be used to create controllers.
/// </summary>
public IKernel Kernel { get; private set; }
2010-10-12 02:49:27 +00:00
2010-10-18 18:04:07 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="NinjectControllerFactory"/> class.
/// </summary>
/// <param name="kernel">The kernel that should be used to create controllers.</param>
public NinjectControllerFactory(IKernel kernel)
{
Kernel = kernel;
}
2010-10-12 02:49:27 +00:00
2010-10-18 18:04:07 +00:00
/// <summary>
/// Gets a controller instance of type controllerType.
/// </summary>
/// <param name="requestContext">The request context.</param>
/// <param name="controllerType">Type of controller to create.</param>
/// <returns>The controller instance.</returns>
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if(controllerType == null)
{
// let the base handle 404 errors with proper culture information
return base.GetControllerInstance(requestContext, controllerType);
}
2010-10-12 02:49:27 +00:00
2010-10-18 18:04:07 +00:00
var controller = Kernel.TryGet(controllerType) as IController;
2010-10-12 02:49:27 +00:00
2010-10-18 18:04:07 +00:00
if (controller == null)
return base.GetControllerInstance(requestContext, controllerType);
2010-10-12 02:49:27 +00:00
2010-10-18 18:04:07 +00:00
/*
var asyncController = controller as AsyncController;
if (asyncController != null)
{
asyncController.ActionInvoker = this.CreateAsyncActionInvoker();
}5
2010-10-18 18:04:07 +00:00
else
{
var standardController = controller as Controller;
if (standardController != null)
standardController.ActionInvoker = CreateActionInvoker();
}*/
2010-10-12 02:49:27 +00:00
2010-10-18 18:04:07 +00:00
return controller;
}
2010-10-12 02:49:27 +00:00
2010-10-18 18:04:07 +00:00
/*
/// <summary>
/// Creates the action invoker.
/// </summary>
/// <returns>The action invoker.</returns>
protected virtual NinjectActionInvoker CreateActionInvoker()
{
return new NinjectActionInvoker(Kernel);
}
2010-10-12 02:49:27 +00:00
/// <summary>
/// Creates the action invoker.
/// </summary>
/// <returns>The action invoker.</returns>
2010-10-18 18:04:07 +00:00
protected virtual NinjectAsyncActionInvoker CreateAsyncActionInvoker()
2010-10-12 02:49:27 +00:00
{
2010-10-18 18:04:07 +00:00
return new NinjectAsyncActionInvoker(Kernel);
}*/
2010-10-12 02:49:27 +00:00
}
}