简体   繁体   English

打破类中所有依赖项的最简单,最快捷的方法

[英]Simplest, fastest way to break out all dependencies from a class

When working with legacy code, and trying to create tests, I often break out dependencies from classes or methods so I can write unit tests using mocks for these dependencies. 在使用遗留代码并尝试创建测试时,我经常从类或方法中分离出依赖关系,因此我可以使用模拟来为这些依赖项编写单元测试。 Dependencies most often come in the form of calls to static classes and objects created using the new keyword in the constructor or other locations in that class. 依赖关系通常以调用静态类和使用构造函数中的new关键字或该类中其他位置创建的对象的形式出现。

In most cases, static calls are handled either by wrapping the static dependency, or if its a singleton pattern (or similar) in the form of StaticClass.Current.MethodCall() passing that dependency by its interface go the constructor instead. 在大多数情况下,静态调用通过包装静态依赖项来处理,或者如果它以StaticClass.Current.MethodCall()形式的单例模式(或类似)通过其接口传递该依赖项而改为构造函数。

In most cases, uses of the new keyword in the constructor is simply replaced by passing that interface in the constructor instead. 在大多数情况下,只需在构造函数中传递该接口即可替换构造函数中new关键字的使用。

In most cases, uses of the new keyword in other parts of the class, is handled either by the same method as above, or by if needed create a factory, and pass the factory's interface in the constructor. 在大多数情况下,在类的其他部分中使用new关键字可以通过与上面相同的方法处理,也可以根据需要创建工厂,并在构造函数中传递工厂的接口。

I always use Resharpers refactoring tools to help me all of these break-outs, however most things are still manual labour (which could be automated), and for some legacy classes and methods that can be a very very tedious process. 我总是使用Resharpers重构工具来帮助我解决所有这些问题,但是大多数事情仍然是手工劳动(可能是自动化的),对于一些遗留的类和方法来说,这可能是一个非常繁琐的过程。 Is there any other refactoring plugins and/or tools which would help me in this process? 是否有任何其他重构插件和/或工具可以帮助我完成此过程? Is there a "break out all depencencies from this class in a single click" refactoring tool? 是否“通过一次点击”从重构工具中突破了所有依赖性? =) =)

It sounds to me like all these steps are common for many developers and a common problem, and before I attempt writing plugin to Resharper or CodeRush, I have to ask, because someone has probably already attempted this.. 听起来像所有这些步骤对于许多开发人员而言是常见的并且是一个常见问题,在我尝试将插件写入Resharper或CodeRush之前,我不得不问,因为有人可能已经尝试过这个...

ADDED: 添加:

In reflection to answers below: even if you might not want to break out everything at once (one click total break out might cause more problems than it helps) still being able to simply break out 1 methods dependencies, or 1-2 dependencies easily, would be of big difference. 反思下面的答案:即使您可能不想一次性突破所有内容(一次点击总突破可能会导致更多问题而不是它有帮助)仍然能够简单地突破1个方法依赖关系,或轻松地突破1-2个依赖关系,会有很大的不同。

Also, refactoring code has a measure of "try and see what happens just to learn how everything fits together", and a one click total break out would help that process tons, even if you dont check that code in.. 此外,重构代码有一个“尝试,看看只是为了学习所有东西是如何组合在一起发生的事情”的衡量标准,并且一键完全突破将有助于这个过程吨,即使你不检查该代码..

I don't think there is any tool that can automate this for you. 我认为没有任何工具可以为您自动化。 Working with legacy code means -as you know- changing code with little steps at a time. 使用遗留代码意味着 - 如您所知 - 一次只需几步就可以更改代码。 The steps are often deliberately small to prevent errors from being made. 这些步骤通常都很小,以防止出错。 Usually the first change you should make is one that makes that code testable. 通常,您应该进行的第一个更改是使该代码可测试的更改。 After you've written the test you change that part of the code in such way that you fix the bug or implement the RFC. 在编写测试之后,您可以通过修复错误或实现RFC来更改代码的这一部分。

Because you should take small steps I believe it is hard to use a refactoring tool to magically make all your dependencies disappear. 因为你应该采取小步骤,我相信很难使用重构工具神奇地使所有依赖关系消失。 With legacy systems you would hardly ever want to make big changes at once, because the risk of breaking (and not finding out because of the lack of tests) is too big. 对于遗留系统,您几乎不需要立即进行大的改变,因为破坏的风险(并且由于缺乏测试而没有发现)太大了。 This however, doesn't mean refactoring tools aren't useful in this scenario. 但是,这并不意味着重构工具在这种情况下无用。 On the contrary; 反之; they help a lot. 他们帮了很多忙。

If you haven't already, I'd advise you to read Michael Feathers' book Working Effectively with Legacy Code . 如果您还没有,我建议您阅读Michael Feathers的“ 有效使用遗留代码 ”一书。 It describes in great details a series of patterns that help you refactor legacy code to a more testable system. 它详细描述了一系列模式,可帮助您将遗留代码重构为更易测试的系统。

Good luck. 祝好运。

When it comes to static call dependencies, you might want to check out Moles . 在涉及静态调用依赖项时,您可能需要查看Moles It's able to do code injection at run-time to stub out any static or non-virtual method call with your own test implementation. 它能够在运行时执行代码注入,以使用您自己的测试实现来存根任何静态或非虚拟方法调用。 This is handy for testing legacy code that wasn't designed using testable dependency-injected interfaces. 这对于测试未使用可测试的依赖注入接口设计的遗留代码非常方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件夹递归输出为树的最快,最简单的方法 - Fastest and simplest way to output Folders Recursion as tree 使用.Net绘制解析树的最简单,最快捷的方法是什么? - What's the simplest and fastest way to draw a parse tree using .Net? silverlight 4-在UI线程上调度工作的最快/最简单的方法? - silverlight 4 - fastest/simplest way of scheduling work on the UI thread? 在没有HBM文件的情况下设置NHibernate / LINQ的最快速最简单的方法 - Fastest and simplest way to set up NHibernate/LINQ without HBM files 从列表中选择所有字符串的最快方法 - Fastest way to select all strings from list starting from 将数据从DataSet中的所有DataTable中的列插入List的最快方法 - Fastest way to insert data from a column in all DataTables in a DataSet to a List 从数据行 C# 为类分配属性的最快方法 - Fastest way to assign properties to class from datarow C# 确定类x是否派生自类y的最简单方法是什么? (C#) - Simplest way to determine if class x is derived from class y? (c#) 如何从 IDisposable 类内部突破使用块 - How to break out of using block from inside IDisposable class 处理类中事件的最简单方法 - Simplest way to handle creating events in a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM