Ninject.MVC3,的NuGet,WebActivator噢,我的 [英] Ninject.MVC3, Nuget, WebActivator oh my

查看:280
本文介绍了Ninject.MVC3,的NuGet,WebActivator噢,我的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置Ninject做一个简单的测试,以及演示使用的NuGet了易于安装。我想解决一个示例服务。

 公共接口ITestService
{
    串的GetMessage();
}
公共类TestService的:ITestService
{
    公共字符串的GetMessage(){返回的Hello World; }
}

我运行的NuGet安装封装NinjectMVC3 ....好听滴NinjectMVC3.cs到我App_Start文件夹,装饰着一些WebActivator属性,把它全部调入。

接下来添加我在NinjectMVC3.RegisterServices方法绑定:

 私有静态无效RegisterServices(内核的iKernel)
{
   kernel.Bind&所述; ITestService方式>()到< TestServiceOne>();
}

现在我想'使用'Ninjet来解决我的ITestService。

 公众的ActionResult指数()
{
  // [Ninject,给我我需要的服务]
  ITestService服务=?  ViewBag.Message = service.GetMessage();
  返回查看();
}

有另一部分设置Ninject?
我是否需要提供一个解析器?

什么是code,我需要解决我ITestService。

thanksasking帮助»

** * * * * * * * * * * *更新:* * * * * * * * * * * * * * * * **

感谢您对'的控制器构造'注入的巨大反应。
简单地增加与ITestServcice作为参数的构造.... BAMM!

 私人ITestService _service;公众的HomeController(ITestService服务)
{
  _Service =服务
}
公众的ActionResult指数()
{
  ViewBag.Message = _service.GetMessage();
  返回查看();
}

现在什么是理想的解决方案,当我需要直接得到内核。

  VAR内核= ... //去抓住从内核????? (感谢犹大)。
ITestService服务= kernel.Get< ITestService>();

我可以看到NinjectMVC3类创建内核,但不持有或接触到它的引用,也可以找到一个明显的类/法得到内核。

我假设你是一个Ninject方式得到它,但不能肯定。

** * * * * * * * * * * *(最终)更新:* * * * * * * * * * * * * * * * **

有关问题的答案和意见再次感谢....

更正:该NinjectMVC3类创建内核,并持有确实参考了引导程序,它作为一个的iKernel就可以了。

所以我增加了一个解析的方法给App_Start / NinjectMVC3类...伟大工程。

 公共静态类NinjectMVC3 ///由NinjectMVC3的NuGet Packagae创建
{
   //添加方法,以提供一个全球性的解析器。
   公共静态ŧ解决< T>()
   {
       返回bootstrapper.Kernel.Get< T>();
   }
}


解决方案

添加ITestService作为构造参数到控制器:

私人ITestService服务;公共myController的(ITestService服务)
{
   this.service =服务;
}公众的ActionResult指数()
{
  ViewBag.Message = this.service.GetMessage();
  返回查看();
}

Ninject自动注入ITestService到控制器。然后使用服务领域的指数方法内。


另外,如果你不想Ninject注入到你的控制器构造,可以保持周围创建了内核,那么你的指数方法里面,你叫kernel.Get< ITestService>()抢一个实例:

公众的ActionResult指数()
{
  VAR内核= ... //去抓住我们的应用程序启动时创建回内核。
  ITestService服务= kernel.Get< ITestService>();  ViewBag.Message = service.GetMessage();
  返回查看();
}

看一看 Ninject依赖注入在控制器MVC3

I want to setup Ninject to do a simple test, as well as demonstrate the ease-of-setup using Nuget. I want to resolve a sample service.

public interface ITestService
{
    string GetMessage();
}
public class TestService : ITestService 
{
    public string GetMessage() { return "hello world"; }
}

I run the NuGet Install-Package NinjectMVC3 .... it nicely drops NinjectMVC3.cs into my App_Start folder, decorated with some WebActivator attributes to get it all loaded.

Next I add my binding in the NinjectMVC3.RegisterServices method:

private static void RegisterServices(IKernel kernel)
{
   kernel.Bind<ITestService>().To<TestServiceOne>();
}

Now I want to 'use' Ninjet to resolve my ITestService.

public ActionResult Index()
{
  //[Ninject, give me the service I need]
  ITestService service = ??? 

  ViewBag.Message = service.GetMessage();
  return View();
}

Is there another part to setting up Ninject? Do I need to provide a resolver?

What is the code I need to resolve my ITestService.

thanksasking help »

** * * * * * * * * * * * Update: * * * * * * * * * * * * * * * * **

Thanks for the great responses regarding 'controller constructor' injection. Simply adding a constructor with the ITestServcice as a param .... BAMM !!!

private ITestService _service;

public HomeController(ITestService service)
{
  _service = service
}
public ActionResult Index()
{
  ViewBag.Message = _service.GetMessage();
  return View();
}

Now what is the ideal solution for, when I need to get the Kernel directly.

var kernel = ... // Go grab the kernel from ????? (thanks Judah).
ITestService service = kernel.Get<ITestService>();

I can see the NinjectMVC3 class creates the Kernel, but does not hold or expose a reference to it, nor can I find a obvious class/method to 'get the kernel'.

I assume thee is a Ninject way to get it, but not sure.

** * * * * * * * * * * * (Final) Update: * * * * * * * * * * * * * * * * **

Thanks again for the answers and comments ....

Correction: The NinjectMVC3 class creates the Kernel, and does hold reference to the 'Bootstrapper', which as a IKernel on it.

So I added a 'resolve' method to the App_Start/NinjectMVC3 class ... works great.

public static class NinjectMVC3 /// created by the NinjectMVC3 NuGet  Packagae
{
   // add method to provide a global resolver.   
   public static T Resolve<T>()
   {
       return bootstrapper.Kernel.Get<T>();
   }
}

解决方案

Add the ITestService as a constructor parameter to your controller:

private ITestService service;

public MyController(ITestService service)
{
   this.service = service;
}

public ActionResult Index()
{
  ViewBag.Message = this.service.GetMessage();
  return View();
}

Ninject will automatically inject the ITestService into your controller. Then use the service field inside your Index method.


Alternately, if you don't want Ninject to inject into your controller constructor, you can keep around the kernel you create, then inside your Index method, you call kernel.Get<ITestService>() to grab an instance:

public ActionResult Index()
{
  var kernel = ... // Go grab the kernel we created back in app startup.
  ITestService service = kernel.Get<ITestService>();

  ViewBag.Message = service.GetMessage();
  return View();
}

Have a look at Ninject dependency inject for controllers in MVC3.

这篇关于Ninject.MVC3,的NuGet,WebActivator噢,我的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆