简体   繁体   English

体系结构层图验证在.Net Core中不起作用

[英]Architecture layer diagram validation not working in .Net core

I'm trying to setup a simple architecture layer diagram to validate that we don't break our layering rules by using namespaces/dll's from incorrect layers. 我正在尝试建立一个简单的体系结构层图,以验证我们是否通过使用来自不正确层的名称空间/ dll违反了分层规则。

I setup a simple solution that has a console app, and 4 dll's (all using .Net Core). 我设置了一个简单的解决方案,该解决方案具有一个控制台应用程序和4个dll(均使用.Net Core)。 The console app calls into Dll1, Dll1 calls into Dll2, Dll2 calls into Dll3, etc. Then I setup a layer diagram to enforce this layering and try to have the console app call into Dll 4 and expected to get an error; 控制台应用程序调用Dll1,Dll1调用Dll2,Dll2调用Dll3,依此类推。然后,我设置了一个图层图以强制执行此分层,并尝试将控制台应用程序调用到Dll 4,并期望得到错误。 however, I don't. 但是,我没有。

If I change this to use a windows form app (instead of the .net core console app) and change all the dll's to be .net standard, then I do get the error enforcing the layer diagram. 如果我将其更改为使用Windows窗体应用程序(而不是.net核心控制台应用程序)并将所有dll都更改为.net标准,则执行层图时会出现错误。

I've tried adding the Microsoft.DependencyValidation.Analyzers NuGet package to the .net core console app, but that still doesn't work. 我尝试将Microsoft.DependencyValidation.Analyzers NuGet程序包添加到.net核心控制台应用程序,但这仍然行不通。 Is this a problem in .Net Core? .Net Core中有问题吗? Or something I'm missing? 还是我想念的东西?

I can't take the credit for this answer, however I found this solution buried in GitHub. 我不能相信这个答案,但是我发现这个解决方案埋在GitHub中。 The manual workaround is listed >here in github< . 手动解决方法在github <中列出。 It got the Dependency Validation Diagram working fine for me in dotnet core. 它使依赖验证图在dotnet核心中对我来说运行良好。

Dependency validation works fine for old-style MSBuild projects, and the only reason it appears not to work for new-style MSBuild projects is that dependency validation designer doesn't add the NuGet package and the link to the model file because it doesn't recognise the new-style projects as being C#/VB projects. 依赖关系验证对于旧式MSBuild项目效果很好,并且它似乎不适用于新式MSBuild项目的唯一原因是,依赖关系验证设计器不会添加NuGet包和指向模型文件的链接,因为它不会添加将新型项目识别为C#/ VB项目。

This is purely an IDE issue - the dependency validation designer doesn't know the GUIDs for the new-style C# and VB project types, so it doesn't show a gold bar to fix up the project files. 这纯粹是一个IDE问题-​​依赖关系验证设计器不知道新型C#和VB项目类型的GUID,因此它没有显示固定项目文件的金条。 The underlying validation mechanism is just using Roslyn analyzers and linked files, both of which are fully supported in MSBuild15. 底层的验证机制仅使用Roslyn分析器和链接文件,MSBuild15完全支持这两种方法。

Manual workaround: For each .Net Standard/Core project that you want to validate: 手动解决方法:对于要验证的每个.Net Standard / Core项目:

Add a reference to the Microsoft.Dependency.Validation.Analyzer NuGet package Add the layer diagram as a linked file with the ItemGroup type of AdditionalFiles 添加对Microsoft.Dependency.Validation.Analyzer NuGet包的引用将图层图添加为具有AdditionalGroups的ItemGroup类型的链接文件

eg 例如

 <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.1</TargetFramework> </PropertyGroup> <!-- Add support for Layer validation --> <ItemGroup> <PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.9.0" /> <!-- Include the layer model as an additional file that will be passed to the analyzer /> <AdditionalFiles Include="..\\LayerTest1\\DependencyValidation1.layerdiagram"> <Link>DependencyValidation1.layerdiagram</Link> <Visible>False</Visible> </AdditionalFiles> </ItemGroup> </Project> 

Background 背景

The dependency validation engine was re-written in VS2017 to use Roslyn analyzers, rather than a separate command line executable. 依赖关系验证引擎在VS2017中进行了重写,以使用Roslyn分析器,而不是单独的命令行可执行文件。 One implication of this is that every code project being validated now needs to have a reference to the NuGet package containing the analyzers, and also a pointer to the layer model file. 这就意味着现在每个要验证的代码项目都需要引用包含分析器的NuGet包,以及指向图层模型文件的指针。 This needs to be added as a linked file so there is only one copy of the model that is shared between all projects, and it needs to belong to the AdditionalFiles ItemGroup, as this is the mechanism used by Roslyn to pass additional configuration to analyzers. 这需要作为链接文件添加,因此该模型在所有项目之间仅共享一个副本,并且它必须属于AdditionalFiles ItemGroup,因为这是Roslyn用来将其他配置传递给分析器的机制。

(Aside: another implication is that the only way to reliably see all dependency validation errors in the IDE is to rebuild the solution. Otherwise, VS will try not build projects that haven't changed, so the Roslyn analyzers won't be called -> no validation errors produced). (此外:另一个含义是,在IDE中可靠地查看所有依赖项验证错误的唯一方法是重建解决方案。否则,VS将尝试不构建未更改的项目,因此不会调用Roslyn分析器- >未产生验证错误)。

"Projects need to be updated" gold bar “项目需要更新”金条

If a solution contains a dependency validation diagram, the dependency validation designer in the Visual Studio IDE will scan the solution for project types it recognises as containing C# or VB code, and check that those projects contain the analyzer NuGet reference and the link to the model file. 如果解决方案包含依赖关系验证图,则Visual Studio IDE中的依赖关系验证设计器将扫描解决方案以查找其识别为包含C#或VB代码的项目类型,并检查这些项目是否包含分析器NuGet参考以及模型的链接。文件。 If they don't then the "[o]ne or more projects needs to be updated to perform dependency validation" gold bar will be shown. 如果没有,则将显示“需要更新一个或多个项目以执行依赖关系验证”金条。

However, the dependency validation designer only knows about the old-style C# and VB project types (GUIDs FAE04EC0-301F-11D3-BF4B-00C04F79EFBC and F184B08F-C81C-45F6-A57F-5ABD9991F28F respectively). 但是,依赖性验证设计器仅了解旧式C#和VB项目类型(分别为GUID FAE04EC0-301F-11D3-BF4B-00C04F79EFBC和F184B08F-C81C-45F6-A57F-5ABD9991F28F)。 It doesn't know about the new-style C# and VB projects (9A19103F-16F7-4668-BE54-9A1E7A4F7556 and 778DAE3C-4631-46EA-AA77-85C1314464D9) because they were released after VS2017 RTM. 它不知道新式的C#和VB项目(9A19103F-16F7-4668-BE54-9A1E7A4F7556和778DAE3C-4631-46EA-AA77-85C1314464D9),因为它们是在VS2017 RTM之后发布的。

https://docs.microsoft.com/en-us/visualstudio/modeling/layer-diagrams-reference?view=vs-2017 has the note: https://docs.microsoft.com/zh-cn/visualstudio/modeling/layer-diagrams-reference?view=vs-2017有注释:

Dependency diagrams are not supported for .NET Core projects in Visual Studio 2017. Visual Studio 2017中的.NET Core项目不支持依赖关系图。

Building on the answer that David gave you can also add an Directory.Build.props to the solution root folder with this content: 在David给出的答案的基础上,还可以将Directory.Build.props添加到具有以下内容的解决方案根文件夹中:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- Add support for Layer validation -->
  <ItemGroup>
    <PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.10.0" />

    <!-- Include the layer model as an additional file that will be passed to the analyzer /> -->
    <AdditionalFiles Include="$(MSBuildThisFileDirectory)Architecture\DependencyValidation.layerdiagram">
      <Link>DependencyValidation1.layerdiagram</Link>
      <Visible>False</Visible>
    </AdditionalFiles>
  </ItemGroup>
</Project>

This will add the dependency validation analyzer and diagram to all projects in your solution. 这会将依赖性验证分析器和图表添加到解决方案中的所有项目。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM