{"id":26185,"date":"2016-11-30T01:57:26","date_gmt":"2016-11-30T09:57:26","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/?p=26185"},"modified":"2020-02-24T06:47:04","modified_gmt":"2020-02-24T14:47:04","slug":"live-dependency-validation-in-visual-studio-2017","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/live-dependency-validation-in-visual-studio-2017\/","title":{"rendered":"Live Dependency Validation in Visual Studio 2017"},"content":{"rendered":"<p>Last month we <a href=\"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2016\/10\/07\/live-architecture-dependency-validation-in-visual-studio-15-preview-5\/\">announced<\/a> that Visual Studio \u201cDev15\u201d Preview 5 now supported Live Dependency Validation. In this blog post,<\/p>\n<ul>\n<li>I\u2019ll give you an update about:\n<ul>\n<li>an on-demand <a href=\"#_video\">\/\/connect 2016 video<\/a> we\u2019ve recorded about Live Dependency Validation<\/li>\n<li><a href=\"#_improvements\">improvements<\/a> in the experience in Visual Studio 2017 RC<\/li>\n<li><a href=\"#_knownIssues\">known issues<\/a>: what did not make it for RC, but will be available in RTM<\/li>\n<\/ul>\n<\/li>\n<li>Then I\u2019ll provide a bit more details on:\n<ul>\n<li><a href=\"#_dvErrorCodes\">error codes<\/a> for the dependency validation errors<\/li>\n<li><a href=\"#_dvDifferences\">differences<\/a> between Layer validation in Visual Studio 2010-2015 and the live dependency experience in Visual Studio 2017<\/li>\n<\/ul>\n<\/li>\n<li>Finally, I\u2019ll share a couple of <a href=\"#_authoring\">tips in order to author dependency validation diagrams<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4>On demand video about dependency validation<\/h4>\n<p>During the connect 2016 event, we\u2019ve proposed an on-demand video which explains in detail why you\u2019d want to use Dependency Validation and how to do so <a href=\"https:\/\/channel9.msdn.com\/Events\/Connect\/2016\/170\" target=\"_blank\" rel=\"noopener noreferrer\">Validate architecture dependencies with Visual Studio<\/a>. The video contains a quick review of the topic \u2013 unwanted dependencies are part of your technical debt. Then I remind you of what the experience was in previous versions of Visual Studio Enterprise, and show how this could be improved, I demo the new-real time dependency validation experience that you, as a developer, will benefit from in Visual Studio 2017. I also demo how easy it is to create a dependency diagram. Finally, I describe how users using the current experience can migrate to the new one\nI encourage you to watch this 10 mins video, which will give you a good overview of the scenario and the feature.<\/p>\n<p style=\"text-align: center;\"><iframe width=\"540\" height=\"320\" src=\"https:\/\/channel9.msdn.com\/Events\/Connect\/2016\/170\/player\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">\ufeff<\/span><\/iframe><\/p>\n<h4>Updates on the experience improvements<\/h4>\n<p>The demos in the video were done with Visual Studio 2017 RC. We had fixed a number of bugs since \u201cPreview 5\u201d and improved the user experience:<\/p>\n<ul>\n<li>When upgrading projects to enable live validation, a dialog now expresses progress,\n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/05\/clip_image00125.png\"><img decoding=\"async\" style=\"padding-top: 0px; padding-left: 0px; padding-right: 0px; border-width: 0px;\" title=\"clip_image001\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2016\/11\/clip_image001_thumb11.png\" alt=\"clip_image001\" width=\"476\" height=\"140\" border=\"0\" \/><\/a><\/li>\n<li>When updating a project for live dependency validation, the version of the NuGet package is now upgraded to be the same for all projects (and is the highest version in use),<\/li>\n<li>VB.NET is now fully supported on par with C#,<\/li>\n<li>The addition of a new Dependency Validation project now triggers a project update,<\/li>\n<li>The dependency validation Roslyn analyzer is now better:\n<ul>\n<li>It\u2019s less verbose as we no longer report when you declare an implicitly-typed variable (var in C#). Because the type of the variable is guaranteed to be the same as the type on the right-hand side we were effectively reporting the same error twice<\/li>\n<li>It now reports on delegate invocations.<\/li>\n<\/ul>\n<\/li>\n<li>You are now advised to enable \u201cfull solution analysis\u201d when using live dependency validation: a gold bar appears in the Error List, and opens the options page pointing you at the option to enable or disable the \u201c<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt709421.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">full solution analysis<\/a>\u201d (for C#, and VB). You can permanently dismiss this gold bar if you are not interested in seeing all the architectural issues in your solution.\n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/05\/clip_image0033.jpg\"><img decoding=\"async\" style=\"padding-top: 0px; padding-left: 0px; padding-right: 0px; border-width: 0px;\" title=\"clip_image003\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2016\/11\/clip_image003_thumb3.jpg\" alt=\"clip_image003\" width=\"760\" height=\"152\" border=\"0\" \/><\/a>\nIf you don\u2019t enable \u201cfull solution analysis\u201d the analysis will only be done for the files being edited (this behavior is common to all Roslyn analyzers).<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4>Known issues<\/h4>\n<p>We were not able to fix everything for RC but we are still working on it. The experience will be even better for RTW. In Visual Studio 2017 RC:<\/p>\n<ul>\n<li>saving a dependency Validation diagram no longer triggers the analysis. This is annoying because if you want to see immediately the effect on the issues in the code, of changing the diagram, the only workaround is to close and reload the solution.<\/li>\n<li>the experience is not very good when you enable the \u201cLightweight Solution load\u201d option.<\/li>\n<li>deleting a dependency validation diagram does not remove the corresponding links from C# and VB projects.<\/li>\n<\/ul>\n<p>These have all been fixed for RTW. <a id=\"_dvErrorCodes\"><\/a><\/p>\n<h4>Real time Dependency Validation error messages<\/h4>\n<p>It\u2019s now possible to click on the error code for a dependency validation error message and get an explanation of the issue. The figure below shows the default severity of the rules in the ruleset editor<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/05\/clip_image0053.jpg\"><img decoding=\"async\" style=\"padding-top: 0px; padding-left: 0px; padding-right: 0px; border-width: 0px;\" title=\"clip_image005\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2016\/11\/clip_image005_thumb2.jpg\" alt=\"clip_image005\" width=\"760\" height=\"233\" border=\"0\" \/><\/a> For your convenience, I\u2019ve summarized, in the table below, in which condition you will have which error.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"86\">Error Code<\/td>\n<td valign=\"top\" width=\"526\">When is it reported?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"86\">DV0001<\/td>\n<td valign=\"top\" width=\"526\"><b>Invalid Dependency<\/b>. This issue is reported when a code element (namespace, type, member) mapped to a Layer references a code element mapped to another layer, but there is no dependency arrow between these layers in the dependency validation diagram containing this layers. This is a dependency constraint violation<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"86\">DV1001<\/td>\n<td valign=\"top\" width=\"526\"><b>Invalid namespace name<\/b>. This issue is reported on a code element associated with a layer which \u201cAllowed Namespace Names\u201d property does not contain the namespace in which this code element is defined. This is a naming constraint violation.<\/p>\n<p>Note that the syntax of \u201cAllowed Namespace Names\u201d is to be a semi-colon list of namespaces in which code elements associated with are layer are permitted to be defined.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"86\">DV1002<\/td>\n<td valign=\"top\" width=\"526\"><b>Dependency on unreferenceable namespace<\/b>. This issue is reported on a code element associated with a layer and referencing another code element defined in a namespace which is defined in the \u201cUnreferenceable Namespace\u201d property of the layer. This is a naming constraint violation.<\/p>\n<p>Note that the \u201cUnreferenceable Namespaces\u201d property is defined as a Semi-colon separated list of namespaces that should not be referenced in code elements associated with this layer<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"86\">DV1003<\/td>\n<td valign=\"top\" width=\"526\"><b>Disallowed namespace name<\/b>. This issue is reported on a code element associated with a layer which \u201cDisallowed Namespace Names\u201d property contains the namespace in which this code element is defined. This is a naming constraint violation.<\/p>\n<p>Note that the \u201cDisallowed namespace name\u201d property is defined as a Semi-colon separated list of namespaces in which code elements associated with this Layer should not be defined.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4>Differences between Layer validation in Visual Studio 2010-2015 and Live Dependency Validation in Visual Studio 2017<\/h4>\n<h5>Old layer validation issues vs new Dependency Validation issues<\/h5>\n<p>As we have explained last month, the Dependency Validation experience leverages the existing Layer diagram format with some improved terminology for the layer properties. The error messages generated by the Roslyn analyzers are similar, but we have used different error code because the results can be slightly different. The old issues have an error code starting with AV (<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dd409395.aspx#Understanding and Resolving Layer Validation Errors\" target=\"_blank\" rel=\"noopener noreferrer\">Architecture Validation errors<\/a>) whereas the new issues have an error code starting with DV (<a href=\"#_dvErrorCodes\" target=\"_blank\" rel=\"noopener noreferrer\">Dependency Validation errors<\/a>).<\/p>\n<h5>Why can there be differences?<\/h5>\n<p>To be fair, you might not notice these differences, however, there is a case where you will. This is the case where you have a Visual Studio 2015 solution where you had enabled the legacy Layer validation. Then you migrate this solution, with Visual Studio 2017 Enterprise, to enable live dependency validation. You commit you changes. Later if you, or someone in your team, opens this solution with Visual Studio 2015, and build it, Visual Studio 2016 will start showing both the old issues (AV) and the new issues (DV). In most cases, they will be the same, but not always, so we thought it would be good to explain a bit more about the differences. The underlying reason for these differences is that the legacy validation (Layer) was being performed against the binary code, whereas the new validation is performed against source code. In the same way that you sometimes must add a reference to an assembly declaring a base interface for a class you use (whereas you don\u2019t yourself reference this base interface), the compiler sometimes generates constructs which are not effectively used, but were drawing a dependency in binary, which is not visible in source code. So, the binary caught legacy validation issues which logically should not be there, whereas the new real time dependency validation won\u2019t. This is a bit subtle, and cases are quite rare. (One case I met is when calling a method passing as a parameter a delegate which had an offending return type).<\/p>\n<h5>What dependency issues are caught in Live Dependency Validation?<\/h5>\n<p>Live Dependency Validation for assemblies, <b>types, and namespaces<\/b> is pretty simple: either they are allowed or they are disallowed. The case of generic types is more complex, but makes sense: it\u2019s enough to have at least one unwanted generic type argument to report an issue on the generic type.<\/p>\n<p>As far as members are concerned:<\/p>\n<ul>\n<li><b>Method declarations<\/b>: we check the return type and the parameter types<\/li>\n<li><b>Method invocations<\/b>:\n<ul>\n<li>we check the parent type of the method being called<\/li>\n<li>we check the return type, even if it is ignored i.e. is not assigned to anything<\/li>\n<li>we do <b>not<\/b> check the types of parameters to a method<\/li>\n<li>However, we do check the values of arguments that are passed to a method. This means that if you pass an object which type is not allowed by at least one of the dependency diagrams in the solution, you will get an error. You won\u2019t, however get an error if you pass null.<\/li>\n<\/ul>\n<\/li>\n<li><b>Property reads and writes<\/b>: we check the containing type and the return type i.e. they are a kind of method invocation<\/li>\n<li><b>Fields<\/b>: we check the field type<\/li>\n<\/ul>\n<p>For <b>delegates, <\/b>things get a little more complicated as sometimes we want to validate them as if they were a type, in others we want to validate them as is they were a method invocation.<\/p>\n<p>&nbsp;<\/p>\n<h4>Tips to author a Dependency Validation diagram<\/h4>\n<p>I\u2019ll finish this blog post by sharing a tip on how to author Dependency Validation diagrams. You can, of course, add layers though the toolbox and map them to code elements using the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dd409462.aspx#Layer Explorer\" target=\"_blank\" rel=\"noopener noreferrer\">Layer Explorer<\/a>. But if you have an existing solution, you can do it quicker by Dragging and from Visual Studio explorers and dropping to a Dependency Validation Diagram. You might not know though that this can also be done by Copy\/Paste or Drag and Drop from a Code Map<\/p>\n<h5>Drag and Drop from explorers<\/h5>\n<p>As shown in the video, a recommended way to add layers to a dependency validation diagram is to drag and drop assemblies, references, types and members from the solution explorer, the Class View or the Object Browser. It\u2019s also possible to drag and drop namespaces from the Class View or the Object Browser (they are the only explorers showing namespace).<\/p>\n<h5>Drag and Drop or Copy \/ Paste from a Code Map<\/h5>\n<p>You might also be interested in creating a Dependency Validation diagram as corresponds to the solution. To do that you can:<\/p>\n<ul>\n<li>Generate a Code Map for Solution (from the Architecture menu)<\/li>\n<li>Personally, I use the Code Map Filter Windows to filter out solution folders and \u201cTest Assets\u201d (as I mostly care so about enforcing dependencies in product code).<\/li>\n<li>On the generated Code map, you may also remove the \u201cExternal\u201d node (or expand it to show external assemblies) depending on what you want to do. In the figure below I removed \u201cExternal\u201d. If you are interested in enforcing namespace dependencies as I did, you can expand the assemblies you care about (delete the other ones from the Code Map)<\/li>\n<li>Then finally when you are happy that you have the content you care about on Code Map, you can create a new Dependency Diagram (from the Architecture menu) select all the nodes from the Code Map (either by Ctrl+A, or by the rubber band selection, which you trigger by pressing the shift key before you click \/ drag \/ release the selection), and do a drag and drop or a copy paste to the new Dependency Validation diagram. You will (in RTM, and after doing a bit of manual layout) get the diagram as in the picture below.<\/li>\n<li>Note that in Visual Studio 2017 RC, you\u2019ll need to press the Shift key to get one layer per Code Map node when doing the drag and drop. And you won\u2019t see the dependencies. This was fixed for RTM.<\/li>\n<li>From there you have the current architecture, and you can decide what you want the architecture to be and modify the Dependency Validation diagram accordingly<\/li>\n<\/ul>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/05\/clip_image0074.jpg\"><img decoding=\"async\" style=\"padding-top: 0px; padding-left: 0px; padding-right: 0px; border: 0px;\" title=\"clip_image007\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2016\/11\/clip_image007_thumb3.jpg\" alt=\"clip_image007\" width=\"761\" height=\"459\" border=\"0\" \/><\/a><\/p>\n<h4>In closing<\/h4>\n<p>As usual we\u2019d love to hear your feedback. Don\u2019t hesitate to send feedback directly from Visual Studio (Help | Send Feedback) to report a problem or provide a suggestion.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last month we announced that Visual Studio \u201cDev15\u201d Preview 5 now supported Live Dependency Validation. In this blog post, I\u2019ll give you an update about: an on-demand \/\/connect 2016 video we\u2019ve recorded about Live Dependency Validation improvements in the experience in Visual Studio 2017 RC known issues: what did not make it for RC, but [&hellip;]<\/p>\n","protected":false},"author":112,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,249,251],"tags":[],"class_list":["post-26185","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-open-source","category-security"],"acf":[],"blog_post_summary":"<p>Last month we announced that Visual Studio \u201cDev15\u201d Preview 5 now supported Live Dependency Validation. In this blog post, I\u2019ll give you an update about: an on-demand \/\/connect 2016 video we\u2019ve recorded about Live Dependency Validation improvements in the experience in Visual Studio 2017 RC known issues: what did not make it for RC, but [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/26185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/112"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=26185"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/26185\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=26185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=26185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=26185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}