英文:
How to set up dependency rules?
问题
我想为一个具有多个解决方案和程序集的存储库设置一些新的依赖规则。规则应该是:在任何项目中,所有类型为*.ABC.的程序集都不应该依赖于.XYZ.*。任何违反此规则的情况都应在构建程序集或CI期间被捕获。
1.我尝试过NDepCop。在那里,我创建了一个用于规则的config.NSDepCop文件。但是,由于config.nsdepcop文件的构建操作未设置为C#分析器附加文件,因此Visual Studio无法将config.nsdepcop文件检测为C#分析器文件,并在构建解决方案时分析依赖关系规则。
其他方法包括"自定义Roslyn规则"、"Resharper"、FxCop自定义规则和NDepend,但我对这些几乎一无所知。
是否有人可以帮我解决这个问题?
英文:
I want to set up some new dependency rules for a repo which has multiple solutions and assemblies. Rule should be, such as:- in any project, all the assemblies of type .ABC. should not depend upon .XYZ.. Any violation of such rule should be caught either during building an assembly or during CI.
1.I tried NsDepCop. There I created a config.NSDepCop file for rules. But the ‘config.nsdepcop file’s build action is not set as C# analyzer additional file due to which Visual studio is unable to detect config.nsdepcop file as C# analyzer file and on building the solution the dependency rules analyzed.
Another ways are "Custom Roslyn rules", "Resharper", FxCop custom rules and NDepend but I have little to no idea about these.
Could anyone please help me out here?
答案1
得分: 3
以下是您要翻译的内容:
使用 NDepend,规则是一个特殊的 C# LINQ 查询,具有特殊的规则头部 warnif count > 0
。因此,问题中提到的规则可以如下所示:
// <Name>Assemblies ABC shouldn't use Assemblies XYZ</Name>
warnif count > 0
let asmABC = Application.Assemblies.Where(a => a.Name.Contains("ABC"))
let asmXYZ = Application.Assemblies.Where(a => a.Name.Contains("XYZ"))
from a1 in asmABC.UsingAny(asmXYZ)
select new { a1, xyzUsed = a1.AssembliesUsed.Intersect(asmXYZ) }
当然,您可以根据您的确切上下文来完善 Where(a => ...)
子句。
在构建后和 CI 中运行依赖规则
NDepend 代码规则可以在以下位置进行验证:
- 在 Visual Studio 中
- 通过 VisualNDepend.exe 与 VisualStudio 或 Rider 并排使用
- 在 Azure DevOps 中
- 在 GitHub Action 中
- 在 TeamCity 中
- 在任何其他 CI/CD 技术中(如 Jenkins、Bamboo...)通过 NDepend.Console.exe
使用依赖规则结果来删除匹配的依赖关系
当在 ABC 组件和 XYZ 组件之间发现依赖关系时,规则可以进一步细化以匹配罪魁祸首类和方法。然而,更实际的做法是:
- 将查询结果导出到NDepend 依赖图
- 使用图耦合功能可视化为什么存在 ABC 到 XYZ 组件之间的依赖关系。
英文:
With NDepend a rule is a C# LINQ query with a special rule header warnif count > 0
. Thus the rule mentioned in the question can look like:
// <Name>Assemblies ABC shouldn't use Assemblies XYZ</Name>
warnif count > 0
let asmABC = Application.Assemblies.Where(a => a.Name.Contains("ABC"))
let asmXYZ = Application.Assemblies.Where(a => a.Name.Contains("XYZ"))
from a1 in asmABC.UsingAny(asmXYZ)
select new { a1, xyzUsed = a1.AssembliesUsed.Intersect(asmXYZ) }
Of course you can refine the Where(a => ...)
clauses to your exact context.
Running the Dependency Rule after Building and in CI
NDepend code rule(s) can be validated:
- within Visual Studio,
- side by side with VisualStudio or Rider thanks to VisualNDepend.exe
- within Azure DevOps
- within GitHub Action
- within TeamCity
- within any other CI/CD technology (Jenkins, Bamboo...) thanks to NDepend.Console.exe
Using the Dependency Rule result to remove a matched dependency
The rule could be refined to match culprit classes and methods when a dependency is found between an ABC assembly and a XYZ assembly. However it is more practicable to:
- export the query result to the NDepend Dependency Graph
- use the Graph coupling feature to visualize why a dependency exists between an ABC to XYZ assembly.
答案2
得分: 2
有一个项目文件片段(来源)在 NsDepCop NuGet 包中,它会自动将项目文件夹中的 'config.nsdepcop' 文件标记为 'C# 分析器附加文件'。所以它应该可以正常工作。
如果不能正常工作,请在 https://github.com/realvizu/NsDepCop/issues 上报问题,以便我们请求更多信息来重现问题。
英文:
There's a project file snippet<sup>(source)</sup> in the NsDepCop nuget package that automatically marks the 'config.nsdepcop' file in the project folder as 'C# analyzer additional file'. So it should just work.
If it doesn't, then please open an issue at https://github.com/realvizu/NsDepCop/issues so we can ask for more info for reproducing the problem.
<hr>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论