3

我正在学习如何在我的 asp.net MVC 4 Web 应用程序中使用 Ninject。我不确定我的概念是否正确。这就是我所做的。

1) 使用 Manage NuGet 安装 Ninject.MVC3

2) 在 NinjectWebCommon.cs 中添加以下代码(该文件由 Nuget 自动添加到 App_start 文件夹中)

  private static void RegisterServices(IKernel kernel)
    {
        **kernel.Bind<IProductRepository>.To<ProductRepository>;**
    }  

3) 控制器代码

Public Class ProductController
Inherits System.Web.Mvc.Controller

Private _rProduct As IProductRepository
'
' GET: /Product

Sub ProductController(ProductRepository As IProductRepository)
    _rProduct = ProductRepository
End Sub


Function ProductList() As ActionResult
    Return View(_rProduct.GetProducts())
End Function
End Class

4) IProductRepository 代码

Public Interface IProductRepository
Function GetProducts() As IQueryable(Of Product)
End Interface

5) ProductRepository 代码

Public Class ProductRepository
Implements IProductRepository

Public Function GetProducts() As IQueryable(Of Product) Implements   IProductRepository.GetProducts
    Return (New List(Of Product)() From {
      New Product() With {.Name = "Football", .Price = 25},
        New Product() With {.Name = "Surf board", .Price = 179},
        New Product() With {.Name = "Running shoes", .Price = 95}
    }.AsQueryable())
End Function
End Class

当我调试时,控件不会转到 NinjectWebCommon.cs 中 RegistryServices() 中的断点,而是转到 ProductController 中的 ProductList()。此时 _rProduct 什么都没有。你能解释一下发生了什么吗?

谢谢

4

2 回答 2

3

根据此页面https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application您需要执行一些额外的步骤,因为您使用的是 VB.Net。

注意:如果您使用 VB.NET 开发 MVC3 应用程序,那么只需几个额外的步骤即可使一切按预期工作:

  • 在 App_Start 中:将 NinjectMVC3.cs 重命名为 NinjectMVC3.vb
  • 用此要点中提供的内容替换 NinjectMVC3.vb 的内容:https ://gist.github.com/923618

编辑:这是我开始工作的 NinjectWebCommon.vb。记下命名空间(我使用“VbMvc”作为我的项目名称)。我没有修改 Global.asax.vb 并且在 RegisterServices 中命中了断点。

Imports Microsoft.Web.Infrastructure.DynamicModuleHelper
Imports Ninject.Web.Common
Imports Ninject.Web
Imports Ninject
Imports Ninject.Web.Mvc

<Assembly: WebActivator.PreApplicationStartMethod(GetType(VbMvc.App_Start.NinjectWebCommon), "StartNinject")> 
<Assembly: WebActivator.ApplicationShutdownMethodAttribute(GetType(VbMvc.App_Start.NinjectWebCommon), "StopNinject")> 

Namespace VbMvc.App_Start
    Public Module NinjectWebCommon
        Private ReadOnly bootstrapper As New Bootstrapper()

        ''' <summary>
        ''' Starts the application
        ''' </summary>
        Public Sub StartNinject()
            DynamicModuleUtility.RegisterModule(GetType(NinjectHttpModule))
            DynamicModuleUtility.RegisterModule(GetType(OnePerRequestHttpModule))
            bootstrapper.Initialize(AddressOf CreateKernel)
        End Sub

        ''' <summary>
        ''' Stops the application.
        ''' </summary>
        Public Sub StopNinject()
            bootstrapper.ShutDown()
        End Sub

        ''' <summary>
        ''' Creates the kernel that will manage your application.
        ''' </summary>
        ''' <returns>The created kernel.</returns>
        Private Function CreateKernel() As IKernel
            Dim kernel = New StandardKernel()

            kernel.Bind(Of Func(Of IKernel))().ToMethod(Function(ctx) Function() New Bootstrapper().Kernel)
            kernel.Bind(Of IHttpModule)().To(Of HttpApplicationInitializationHttpModule)()

            RegisterServices(kernel)
            Return kernel

        End Function

        ''' <summary>
        ''' Load your modules or register your services here!
        ''' </summary>
        ''' <param name="kernel">The kernel.</param>
        Private Sub RegisterServices(ByVal kernel As IKernel)
            ''kernel.Load(New Bindings.ServiceBindings(), New Bindings.RepositoryBindings(), New Bindings.PresentationBindings(), New Bindings.CrossCuttingBindings())
        End Sub
    End Module
End Namespace
于 2013-10-22T18:08:16.297 回答
2

您可以在应用Global.asax程序启动(或回收)时调用一次RegisterServices()Application_Start()方法调用它,而不是为每个 http 请求调用。

如果要调试,请按照此处Application_Start()的说明进行操作。

于 2013-10-22T18:12:33.527 回答