ASP.NET MVC - 在控制器之外使用Ninject绑定

时间:2011-03-25 12:13:25

标签: asp.net asp.net-mvc dependency-injection ninject

我正在使用ASP.NET MVC3和Ninject。我在“AppStart_NinjectMVC3.cs”中设置了标准代码实现,它设置了绑定并向DependencyResolver添加了一个内核,如下所示:

    public static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IUserRepository>().To<UserRepository>();
        ...
    }

    public static void Start() {
        IKernel kernel = new StandardKernel();
        RegisterServices(kernel);
        DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));
    }

一切都在我的控制器中运行良好 - 依赖性正在得到解决。

我希望能够在控制器之外和MVC堆栈之外使用Ninject和这些绑定。例如,我有一堆常规的aspx页面,其中我想使用我的ninject内核,还有一些代码挂起了global.asax。

我可以在其他地方重用我的Ninject内核,还是需要在Global.asax appstart中注册内核?

2 个答案:

答案 0 :(得分:2)

http://teamcity.codebetter.com上找到的当前开发版本支持普通aspx页面的侧面使用,mvc和wcf。你可能想看看这个。

请注意,这是一个开发版本,未经过良好测试。不过,我认为它应该非常稳定。但由于它正在进行中,因此界面可以改变。在编写关于此更改的Ninject 2.4预览博客之前,我也不会给予很多支持。

你需要

  • Ninject
  • Ninject.Web.Common
  • Ninject.Web
  • Ninject.Web.MVC3

答案 1 :(得分:0)

我在ASP.NET MVC应用程序中使用了Ninject MVC Extension

以下是我实现我认为你想要实现的目标的方式。

<强>的Global.asax.cs:

public class MvcApplication : NinjectHttpApplication
{
    /// <summary>
    /// Overridden Ninject method that is called once the application has started and is initialized
    /// </summary>
    protected override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);

        // Tell the MVC Framework to use our implementation of metadataprovider.
        ModelMetadataProviders.Current = new XXX.myNamespace.MetadataProvider();

        // Tell the MVC Framework to use our CartModelBinder class
        ModelBinders.Binders.Add(typeof(Cart), new CartModelBinder());
    }


    /// <summary>
    /// Establish a reference to our DIFactory object
    /// <remarks>
    /// This application currently uses Ninject for dependency injection.
    /// </remarks>
    /// </summary>
    /// <returns></returns>
    protected override IKernel CreateKernel()
    {
        return DIFactory.GetNinjectFactory();
    }

    // snip... additional global.asax.cs methods
}

<强> DIFactory.cs:

/// <summary>
/// This class is used as a container for dependency injection throughout the entire application
/// </summary>
public class DIFactory
{
    public static IKernel _kernel = null;
    /// <summary>
    /// Method used to create a single instance of Ninject's IKernel
    /// </summary>
    /// <returns>IKernel</returns>
    public static IKernel GetNinjectFactory()
    {
        if (_kernel == null)
        {
            var modules = new INinjectModule[]
            {
                new ServiceModule()
            };

            _kernel = new StandardKernel(modules);
        }
        return _kernel;
    }

    /// <summary>
    /// Method used as a service locator for the IConfiguration interface
    /// </summary>
    /// <returns></returns>
    public static IConfiguration CreateConfigurationType()
    {
        return _kernel.Get<IConfiguration>();
    }

    // snip....additional public static methods for all other Interafaces necessary
}

<强> ServiceModule.cs:

/// <summary>
/// Configures how abstract service types are mapped to concrete implementations
/// </summary>
internal class ServiceModule : NinjectModule
{
    public override void Load()
    {
        Bind<IConfiguration>().To<XXX.myNamespace.Configuration>();

        // snip... all other bindings to interfaces
    }
}

在控制器以外的其他类中使用:

<强> UserInteraction.cs:

public class UserInteraction : IUserInteraction
{
    private IConfiguration configuration;

    public bool SubmitFeedback(Feedback feedback)
    {
        try
        {
            this.configuration = DIFactory.CreateConfigurationType();
            // snip additional logic...

        }
        catch(Exception ex)
        {
            // snip
        }
    }
 }