Ninject 和像 RhinoMocks 或 Moq 这样的模拟框架有什么区别?我用谷歌搜索了这个,但仍然不清楚.
What is the difference between Ninject and a mocking framework like RhinoMocks or Moq? I Google'd this but it is still unclear.
Ninject 是 .NET 的依赖注入.
RhinoMocks 和 Moq 都是模拟框架.
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.
依赖注入:是一种控制反转的实现(我们称之为).你不要混淆这两者.您正在控制从代码中创建对象.依赖项,例如 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.
}
}
现在到处都是,您将拥有:
Now all over the place, you'll have:
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()
.
一个简单的方法是用户一个工厂方法,它是IOC的另一种形式.
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.
<小时>
模拟框架:天哪,这对我来说就像火箭科学.但史蒂文·桑德森的书有一个绝妙的简单解释.
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 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
声明哪些类要 Export
(或公开)基于 #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.
这篇关于依赖注入和模拟框架之间的区别(Ninject vs RhinoMocks 或 Moq)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!