检测 .NET 中命名空间之间的依赖关系

发布于 2024-07-09 08:05:26 字数 334 浏览 8 评论 0 原文

是否有任何实用程序可以检查一组托管程序集并告诉您一个命名空间中的任何类型是否依赖于另一个命名空间中的任何类型? 例如,假设我有一个 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

微暖i 2024-07-16 08:05:26

到目前为止,我遇到的唯一看起来可以做到这一点的工具是 NDepend,但我想知道是否有更简单的解决方案。

我是该工具 NDepend 的开发人员之一。 请告诉我们您在 NDepend 中发现什么复杂以及您如何为您设想一个更简单的解决方案?

NDepend 提供了 3 种不同的方式来完成您想要的操作:依赖关系矩阵依赖关系图,您也可以编写一些LINQ 查询 (CQLinq) 的代码规则检测命名空间之间的循环,或强制执行某些特定的依赖关系。

例如,假设我有一个 MyApp.BusinessRules 命名空间,并且不希望它直接访问 MyApp.GUI 中的任何内容,但两个命名空间都位于同一程序集中。

为此,可以编写以下 CQLinq 规则,是否可以比这更简单?:

warnif count > 0
let businessRules = Application.Namespaces.WithNameLike("^MyApp.BusinessRules")
let gui = Application.Namespaces.WithNameLike("^MyApp.GUI")

from n in businessRules.UsingAny(gui)
let guidNamespacesUsed = n.NamespacesUsed.Intersect(gui)
select new { n, guidNamespacesUsed }

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.

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

For that, the following CQLinq rule can be written, could it be any simpler than that?:

warnif count > 0
let businessRules = Application.Namespaces.WithNameLike("^MyApp.BusinessRules")
let gui = Application.Namespaces.WithNameLike("^MyApp.GUI")

from n in businessRules.UsingAny(gui)
let guidNamespacesUsed = n.NamespacesUsed.Intersect(gui)
select new { n, guidNamespacesUsed }
人事已非 2024-07-16 08:05:26

老实说,我怀疑 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.

终遇你 2024-07-16 08:05:26

您可以使用 .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

那一片橙海, 2024-07-16 08:05:26

您可以尝试使用 Visual Studio 2010 Ultimate 的 RC 版本来生成 .NET 代码的依赖关系图。 您可以生成所有程序集、命名空间、类或它们的某种组合的图表,也可以使用 Architecture Explorer 选择特定的工件和您想要可视化的关系。

您还可以从依赖关系图或现有工件创建层图,绘制允许的依赖关系,然后将层验证作为 MSBuild 过程的一部分包括在内,以确保不会引入无效的依赖关系:

如何:生成图形文档来自代码http:// /msdn.microsoft.com/en-us/library/dd409453%28VS.100%29.aspx#SeeSpecificSource

您可以使用 Visual Studio Ultimate 通过以下方式探索现有代码中的关系和组织:
生成有向图文档。 这些图表示代码元素及其关系
作为通过链接或边连接的一组节点。 您可以使用这些图表来帮助您
可视化、探索和分析代码。

如何:使用 Architecture Explorer 查找代码http://msdn.microsoft.com/en-us/library/dd409431%28VS.100%29.aspx

您可以使用以下命令选择要可视化的代码的垂直部分或“切片”
架构探索者。 您可以在 Visual Studio 解决方案中探索源代码或
.dll 文件或 .exe 文件中已编译的托管代码。 您可以使用 Architecture Explorer 来浏览
通过安装其他提供程序来访问其他域。 当你找到你想要的代码时
可视化,您可以生成图表来探索该代码中的关系。

如何:从工件创建层图http://msdn.microsoft.com/en-us/library/dd465141%28VS.100%29.aspx

...使用层图来可视化系统的高级架构并验证
该代码符合此设计。 要执行此任务,请组织并关联
将 Visual Studio 解决方案中的工件分解为逻辑、抽象组或层。 这些层
描述这些工件执行的主要任务或系统的主要组件。 箭头
层与层之间表示这些层之间存在或应该存在的相互依赖关系
文物。 要对代码实施架构约束,请描述预期的依赖关系
图表,然后根据图表验证代码。 通过在此使用层图
方式,您可以帮助使代码更易于理解、重用和维护。

图层图 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

You can use Visual Studio Ultimate to explore the relationships and organization in existing code by
generating directed graph documents. These graphs represent code elements and their relationships
as a set of nodes that are connected by links, or edges. You can use these graphs to help you
visualize, explore, and analyze code.

How to: Find Code Using Architecture Explorer: http://msdn.microsoft.com/en-us/library/dd409431%28VS.100%29.aspx

You can select vertical sections or "slices" of code that you want to visualize by using
Architecture Explorer. You can explore source code in a Visual Studio solution or
compiled managed code in .dll files or .exe files. You can use Architecture Explorer to browse
other domains by installing additional providers. When you find the code that you want to
visualize, you can generate graphs to explore the relationships in that code.

How to: Create Layer Diagrams from Artifacts: http://msdn.microsoft.com/en-us/library/dd465141%28VS.100%29.aspx

...use layer diagrams to visualize the high-level architecture of your system and to verify that
the code complies with this design. To perform this task, organize and associate the
artifacts from a Visual Studio solution into logical, abstract groups, or layers. These layers
describe major tasks that these artifacts perform or the major components of your system. Arrows
between the layers represent the interdependencies that exist or should exist between these
artifacts. To enforce architectural constraints on the code, describe the intended dependencies on
the diagram and then validate the code against the diagram. By using layer diagrams in this
manner, you can help make the code easier to understand, reuse, and maintain.

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

Validating code against a layer diagram helps you enforce architectural constraints on the code
as it evolves. When you perform this task, dependencies in the code are compared with dependencies
on the diagram.

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

赠我空喜 2024-07-16 08:05:26

很可能 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.

上课铃就是安魂曲 2024-07-16 08:05:26

反射器就是这样做的。

右键单击命名空间,单击分析,中提琴! :)

Reflector does this.

Right click namespace, click Analyze, viola! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文