且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

依赖注入和模拟框架之间的区别(Ninject与RhinoMocks或Moq)

更新时间:2023-11-03 18:59:16

Ninject 是用于.NET的依赖项注入

RhinoMocks Moq 都是模拟框架。

现在都彼此无关。我确实很难理解两者,所以在这里我要尝试解释。

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

依赖注入:是Inversion of的实现(叫它)控制。您不要混淆两者。您可以控制从代码中创建对象的过程。像 IRepository 这样的依赖项不会由您的类/代码创建,而是由其他人(依赖项注入框架)注入创建的。

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.
 } 
}

现在到处都是:

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

为什么?问问自己为什么首先要创建一个界面?因此,您可以应对更改。现在,当您需要将存储库更改为其他内容时。您必须替换所有具有 new 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;

现在,当您必须更改存储库时,只需更改工厂。 依赖注入通过完成所有工作将其提升到一个新的水平。

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.






模拟框架 :男孩,这对我来说就像火箭科学。但是史蒂文·桑德森(Steven 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";
   }
}

现在,当您通过此 fake 回购。如果您的测试失败了,您就会知道那是其他原因,而不是数据库。

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

我的示例很简单,但是它包含大量接口。您需要编写很多 fake 代码,这会使代码膨胀很多!

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!

因此您可以在此处使用模拟框架编写更少的代码。

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

Moq使用流畅的界面,相当不错。使用Moq看起来像这样:

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.

对于较小的Silverlight应用程序,我使用 MEF (内置于.Net4中)。然后我对导出(或暴露)的类的声明几乎没有 #Ifdef 。 c $ c> #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.

真的希望对您有所帮助。

Really Hope that was helpful.