依赖注入和嘲弄框架的区别(Ninject VS RhinoMock或MOQ) [英] Difference between Dependency Injection and Mocking framework (Ninject vs RhinoMock or Moq)

查看:379
本文介绍了依赖注入和嘲弄框架的区别(Ninject VS RhinoMock或MOQ)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,什么是Ninject与放大器之间的差异;嘲弄的框架,比如RhinoMock或起订量?我google'd这一点,但它目前还不清楚。

So what is the difference between Ninject & a mocking framework like RhinoMock or moq? I google'd this but it is still unclear.

推荐答案

Ninject 是的依赖注入为.NET。

RhinoMocks 和的起订量都是嘲讽的框架。

RhinoMocks and Moq are both mocking frameworks.

现在都有无关,与对方。我真的很难理解这两种所以在这里我去试图解释。

Now both have nothing to do with each other. I really had trouble understanding both so here I go trying to explain.

依赖注入:是一个实现(可以称之为)反转控制的。你不把两者混淆。您正在创建对象从你的code的控制。依赖关系,好比说一个 IRepository 就不会被你的类/ code创建的,而是注射的别人,依赖注入框架。

Dependency Injection: is an implementation (lets call it) of Inversion of Control. You don't confuse the two. You are taking the control of creating an object out of your code. Dependencies, like say a IRepository would not be created by your classes/code but instead injected by someone else, a dependency injection framework.

假设你有

interface IUserRepository
{
 string GetUserName(int id);//one method for simplicity
}

现在你有一个实际的实现:

Now you have an actual implementation:

class MyUserRepo : IUserRepository
{
 string GetUserName(int id)
 {
  //grab your username from your data base here.
 } 
}

现在所有的地方,你就必须:

Now all over the place, you'll have:

IUserRepository repo = new MyUserRepo();//this is bad!!

为什么呢?问问自己,为什么你做摆在首位的接口?所以,你可以应付更改。现在好了,当你需要你的资料库改到别的东西。您必须更换所有的线路有新MyUserRepo()

Why? Ask yourself why you made an interface in the first place? So you can cope with change. Well now, when you need to change your repository to something else. You have to replace all the lines that have new MyUserRepo().

有一个简单的方法是用户一个工厂方法,这是国际奥委会的另一种形式。

A simple method is user a factory method which is another form of IOC.

class RepoFactory
{
 public static IUserRepository UserRepo
 {
  get {return MyUserRepo();}
 } 
}

和使用这样的:

IUserRepository rep = RepoFactory.UserRepo;

现在,当你必须改变你的版本库,你只需要改变你的工厂。 依赖注入按做所有的工作都该到一个新的水平。你并不需要更改code在所有(或者少数的声明)。

Now when you have to change your repository you have to change only your factory. Dependency injection takes this to the next level by doing all the work. You don't need to change the code at all (or maybe a few declarations).

IUserRepository repo; 
//this magically gets the right instance based on some config somewhere.


嘲弄框架:小子,这是像火箭科学对我来说。但史蒂芬Sandersons书中有过辉煌简单的解释。


A Mocking Framework : Boy this was like rocket science to me. But Steven Sandersons book had a brilliant simple explanation.

我们一直在进行着的 IUserRepository

现在你要测试一些复杂的UI /验证任何依赖于 IUserRepository

Now you have to test some complicated UI/Authentication whatever that depends on IUserRepository.

class UserDisplay : UserControl
{
  UserDisplay(IUserRepository repo)
  {//display the username or something here..
  } 
}

现在在您的测试,当你 IUserRepository MyUserRepo 的实例。如果出现问题,你不知道哪里出了问题!是不是你的用户控件或数据库连接?

Now in your test, when you make IUserRepository an instance of MyUserRepo. If something goes wrong you don't know what went wrong! Was it your user control or your database connection?

您需要做有人说测试更具有确定性。

You want make the test more deterministic as someone said.

所以,你犯了一个假的用户资源库。

So you make a fake user repository.

class FakeUserRepo : IUserRepository
{
  public string GetUserName(int id)
  {
    return "FakeUser";
   }
}

所以,现在,当你通过这个回购。如果你测试失败了,你知道这是别的东西,而不是数据库。

So now, when you pass this fake repo. If you're test fails you KNOW it was something else, not the data base.

我的例子很简单,但如果它的大量接口。你需要写很多的的code,它的很多code臃肿!

My example was simple, but if its a large number of Interfaces. You'll need to write a lot of fake code, its a lot of code bloat!

所以,你可以用嘲讽的框架来这里写更少的code。

So you can use a mocking framework to write less code here.

起订量采用了流畅的界面,是相当不错的。使用起订量是这样的:

Moq uses a fluent interface and is quite nice. Using Moq would look like this:

var fakeUserRepo = new Mock<IUserRepository>();
fakeUserRepo.Setup(f => f.GetUserName(It.IsAny<int>)).Returns("FakeUser");
//does the same thing as the class declaration
fakeUserRepo.Object;//this returns fake object of type IUserRepository

创建虚假对象变得轻松了很多=)

Creating fake objects becomes a lot easier =)

现在,我希望你看到如何使用这两种你的优势。您可以使用模拟框架创建假的对象,然后使用依赖注入挂钩的权利对象在正确的时间。

Now I hope your seeing how you can use both to your advantage. You can create your fake objects with a mocking framework, then use dependency injection to hook up the right objects at the right time.

有关我使用 MEF (内置在.NET4)的依赖注入我小的Silverlight应用程序。然后,我没有什么 #IFDEF 在声明它的类导出(或暴露)基于一个的#define 符号。所以,我只是改变一个的#define ,我可以切换我的应用程序在这里和那里使用假类。

For my smaller Silverlight applications I use MEF (Inbuilt in .Net4) for Dependency Injection. And then I have little #Ifdef on the declarations for which classes to Export (or expose) Based on a #define symbol. So I just change one #define and I can switch my app to using fake classes here and there.

真希望这是有帮助的。

这篇关于依赖注入和嘲弄框架的区别(Ninject VS RhinoMock或MOQ)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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