是否有任何实用程序可以检查一组托管程序集并告诉您一个命名空间中的任何类型是否依赖于另一个命名空间中的任何类型? 例如,假设我有一个 MyApp.BusinessRules
命名空间,并且不希望它直接访问 MyApp.GUI
中的任何内容,但两个命名空间都位于同一个程序集中。 我的目标是能够编写自定义 MSBuild 任务来验证各种耦合规则是否未被破坏。
到目前为止,我遇到的唯一看起来可以做到这一点的工具是 NDepend,但我想知道是否有一个更简单的解决方案。
Are there any utilities that can examine a set of managed assemblies and tell you whether any of the types in one namespace depend on any in another? For example, say I have a MyApp.BusinessRules
namespace and don't want it to access directly anything in MyApp.GUI
, but both namespaces are in the same assembly. My goal is to be able to write a custom MSBuild task that verifies that various coupling rules have not been broken.
So far the only tool I have come across that looks like it might do this is NDepend, but I am wondering if there is a simpler solution.
发布评论
评论(6)
我是该工具 NDepend 的开发人员之一。 请告诉我们您在 NDepend 中发现什么复杂以及您如何为您设想一个更简单的解决方案?
NDepend 提供了 3 种不同的方式来完成您想要的操作:依赖关系矩阵、依赖关系图,您也可以编写一些LINQ 查询 (CQLinq) 的代码规则 和 检测命名空间之间的循环,或强制执行某些特定的依赖关系。
为此,可以编写以下 CQLinq 规则,是否可以比这更简单?:
I am one of the developer of the tool NDepend. Please could you let us know what do you find complicated in NDepend and how you imagine a simpler solution for you?
NDepend comes with 3 different ways to do what you want: Dependency Matrix, Dependency Graph and also you can write some Code Rule over LINQ Query (CQLinq) and rules to detect cycle between namespaces, or enforce some particular dependencies.
For that, the following CQLinq rule can be written, could it be any simpler than that?:
老实说,我怀疑 NDepend 将是最简单的方法。
但是,如果您确实不希望一个程序集的各个部分相互引用,那么您几乎肯定应该将该程序集拆分为更多逻辑单元。
I suspect NDepend is going to be the simplest way to go, to be honest.
However, if you really don't want bits of one assembly from referring to each other, you should almost certainly split the assembly up into more logical units.
您可以使用 .NET Reflector 的 DSM 插件 分析命名空间依赖关系(我是其开发人员
)分析程序集后,您可以将项目保存到文件中。 该文件只是具有简单结构的 XML,因此您可以将其传递给脚本进行自定义分析
[更新]:该插件现在以 Visual Studio 插件的形式提供
You can analyze namespace dependencies with the DSM plugin for .NET Reflector ( I'm its developer)
Once the assemblies are analyzed you can save the project to a file. This file is just XML with a simple structure so you can pass it to a script for custom analysis
[Update]: This plugin is now available in form of a Visual Studio Add-In
您可以尝试使用 Visual Studio 2010 Ultimate 的 RC 版本来生成 .NET 代码的依赖关系图。 您可以生成所有程序集、命名空间、类或它们的某种组合的图表,也可以使用 Architecture Explorer 选择特定的工件和您想要可视化的关系。
您还可以从依赖关系图或现有工件创建层图,绘制允许的依赖关系,然后将层验证作为 MSBuild 过程的一部分包括在内,以确保不会引入无效的依赖关系:
如何:生成图形文档来自代码: http:// /msdn.microsoft.com/en-us/library/dd409453%28VS.100%29.aspx#SeeSpecificSource
如何:使用 Architecture Explorer 查找代码:http://msdn.microsoft.com/en-us/library/dd409431%28VS.100%29.aspx
如何:从工件创建层图:http://msdn.microsoft.com/en-us/library/dd465141%28VS.100%29.aspx
图层图 http://i.msdn.microsoft .com/Dd465141.UML_LayerRefReading(en-us,VS.100).png
如何:根据层图验证代码:http://msdn.microsoft.com/en-us/library/dd409395%28VS.100%29.aspx
RC 下载:http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=457bab91-5eb2-4b36-b0f4-d6f34683c62a。
Visual Studio 2010 架构发现与设计 建模工具论坛:http://social.msdn。 microsoft.com/Forums/en-US/vsarch/threads
You can try the RC release of Visual Studio 2010 Ultimate to generate dependency graphs for .NET code. You can generate a graph of all your assemblies, namespaces, classes, or some combination of these, or you can use Architecture Explorer to select specific artifacts and the relationships that you want to visualize.
You can also create layer diagrams from dependency graphs or from the existing artifacts, draw the permitted dependencies, and then include layer validation as part of the MSBuild process to make sure invalid dependencies aren't introduced:
How to: Generate Graph Documents from Code: http://msdn.microsoft.com/en-us/library/dd409453%28VS.100%29.aspx#SeeSpecificSource
How to: Find Code Using Architecture Explorer: http://msdn.microsoft.com/en-us/library/dd409431%28VS.100%29.aspx
How to: Create Layer Diagrams from Artifacts: http://msdn.microsoft.com/en-us/library/dd465141%28VS.100%29.aspx
Layer Diagram http://i.msdn.microsoft.com/Dd465141.UML_LayerRefReading(en-us,VS.100).png
How to: Validate Code Against Layer Diagrams: http://msdn.microsoft.com/en-us/library/dd409395%28VS.100%29.aspx
RC download: http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=457bab91-5eb2-4b36-b0f4-d6f34683c62a.
Visual Studio 2010 Architectural Discovery & Modeling Tools forum: http://social.msdn.microsoft.com/Forums/en-US/vsarch/threads
很可能 Mark 的问题早就解决了,所以这个答案是针对使用 VS Community 的新手的。 最近我遇到了 NsDepCop ,它恰好是免费且有用的。
我在使用 VS 2019 Community 构建的项目中使用它,尽管设置 config.nsdepcop 可能有点麻烦,但它仍然可以工作 - 毕竟它是在编辑 XML 规则。
Most probably Mark has his issue solved long ago, so this answer is for newcomers using VS Community. Recently I encountered NsDepCop out there and it happened to be both free and useful.
I use it in projects I build with VS 2019 Community and it works despite setting
config.nsdepcop
might be somewhat cumbersome - it is editing XML rules after all.反射器就是这样做的。
右键单击命名空间,单击分析,中提琴! :)
Reflector does this.
Right click namespace, click Analyze, viola! :)