{"id":1507,"date":"2011-03-02T22:17:41","date_gmt":"2011-03-02T21:17:41","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=1507"},"modified":"2024-02-13T09:31:58","modified_gmt":"2024-02-13T08:31:58","slug":"ndepend-creating-dependency-violation-warnings","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2011\/03\/ndepend-creating-dependency-violation-warnings\/","title":{"rendered":"Fun with NDepend, Part II: Creating Dependency Violation Warnings"},"content":{"rendered":"<p><a href=\"2011\/02\/fun-with-ndepend-part-one\/\">Last time I gave<\/a> a short overview over the features of <a href=\"http:\/\/ndepend.com\/\">NDepend<\/a>. We know that NDepend can show a detailed dependency analysis your projects.&nbsp; This time I show you how you can write <a href=\"http:\/\/www.ndepend.com\/CQL.htm\">CQL queries<\/a> to find out certain dependency violations.<\/p>\n<div id=\"attachment_1517\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/rules-with-ndepend.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1517\" class=\"size-medium wp-image-1517\" title=\"rules-with-ndepend\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/rules-with-ndepend-300x223.png\" alt=\"Creating Rules With NDepend\" width=\"300\" height=\"223\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/rules-with-ndepend-300x223.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/rules-with-ndepend.png 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1517\" class=\"wp-caption-text\">Creating Rules With NDepend<\/p><\/div>\n<h3>Basic Dependency Operators<\/h3>\n<p>The CQL language has a bunch of \u2018dependency\u2019 operators, like IsUsing, IsUsedBy, IsDirectlyUsing , Implements etc. Those let you find out if a method\/type\/assembly is using a certain other methods\/types\/namespaces. Such an operator has most if the time a companion-operator, like DepthOfIsUsing, DepthOfIsUsedBy etc. A &#8216;dependency-depth&#8221; operator tells you if something is directly used or is a transitive dependency. Here\u2019s example to explain that. Take a look at this small piece of code:<\/p>\n<script src=\"https:\/\/gist.github.com\/851675.js?file=Dependencies.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">namespace NDependExperiments\n{\n    public class DataBaseOperation\n    {\n        public void Store(object obj) {\/** Do stuff **\/}\n    }\n    class BusinessOperations\n    {\n        private readonly DataBaseOperation dbConnection = new DataBaseOperation();\n\n        public void CreateNewUser(string userName){\/** Do stuff **\/}\n    }\n    class Visualisation\n    {\n        private readonly BusinessOperations businessLogic = new BusinessOperations();\n\n        public void Visualize() {\/** Do stuff **\/}\n    }\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            new Visualisation().Visualize();\n        }\n    }\n}<\/code><\/pre><\/noscript>\n<p>Use the IsUsedBy-operator on it and we get this result:<\/p>\n<div id=\"attachment_1509\" style=\"width: 462px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/demo-is-used-by.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1509\" class=\"size-full wp-image-1509\" title=\"demo-is-used-by\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/demo-is-used-by.png\" alt=\"Is UsedBy operator\" width=\"452\" height=\"280\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/demo-is-used-by.png 452w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/demo-is-used-by-300x185.png 300w\" sizes=\"(max-width: 452px) 100vw, 452px\" \/><\/a><p id=\"caption-attachment-1509\" class=\"wp-caption-text\">Is UsedBy operator<\/p><\/div>\n<p>The DepthOfIsUsing tells us how directly an element is used. A value of 1 means that the code directly uses that element. In our example the Progam class directly uses the Visualisation class and hence the DepthOfIsUsing is one. The BusinessOperations is used by the Visualisation class, which is used by the Program class, therefore the depth is two and so on and so forth. The depth tells you over how many indirections something is used. This is very useful to filter out uninteresting relations. For example if you\u2019re only interested in direct dependencies, then you can limit to a certain depth:<\/p>\n<div id=\"attachment_1510\" style=\"width: 579px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/demo-is-using-depth.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1510\" class=\"size-full wp-image-1510\" title=\"demo-is-using-depth\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/demo-is-using-depth.png\" alt=\"using depth\" width=\"569\" height=\"220\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/demo-is-using-depth.png 569w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/demo-is-using-depth-300x115.png 300w\" sizes=\"(max-width: 569px) 100vw, 569px\" \/><\/a><p id=\"caption-attachment-1510\" class=\"wp-caption-text\">Using Depth<\/p><\/div>\n<h2>Update: Show Query Result as Depedency-Graph<\/h2>\n<p>This is quite a hidden feature: You can visualize results of a query as a dependency graph. We use the query from our example application. Run the query, then right-click on the header of the result and choose an visualization:<\/p>\n<div id=\"attachment_1524\" style=\"width: 466px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/export-to-graph.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1524\" class=\"size-full wp-image-1524\" title=\"export-to-graph\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/export-to-graph.png\" alt=\"Show Query Result as Graph\" width=\"456\" height=\"332\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/export-to-graph.png 456w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/export-to-graph-300x218.png 300w\" sizes=\"(max-width: 456px) 100vw, 456px\" \/><\/a><p id=\"caption-attachment-1524\" class=\"wp-caption-text\">Show Query Result as Graph<\/p><\/div>\n<p>And the result:<\/p>\n<div id=\"attachment_1526\" style=\"width: 610px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/depedency-graph-of-query.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1526\" class=\"size-full wp-image-1526\" title=\"depedency-graph-of-query\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/depedency-graph-of-query.png\" alt=\"Graph of a Query Result\" width=\"600\" height=\"207\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/depedency-graph-of-query.png 600w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/depedency-graph-of-query-300x103.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-1526\" class=\"wp-caption-text\">Graph of a Query Result<\/p><\/div>\n<h2>A Real World Use Case<\/h2>\n<p>Now for what can we use this? Well with this we can create our own warnings. Let&#8217;s say we have a larger application which has three layers: &nbsp;The core-layer which contains stuff like database-connections, the business-logic layer which does all our complex logic and the presentation-layer which creates a nice user interface. Now we want to ensure that the presentation-layer never touches the database-stuff directly. For that we can &nbsp;write a little rule to check it:<\/p>\n<div id=\"attachment_1511\" style=\"width: 485px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/dont-use-the-db.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1511\" class=\"size-full wp-image-1511\" title=\"dont-use-the-db\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/dont-use-the-db.png\" alt=\"Dont use the database in views\" width=\"475\" height=\"306\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/dont-use-the-db.png 475w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/dont-use-the-db-300x193.png 300w\" sizes=\"(max-width: 475px) 100vw, 475px\" \/><\/a><p id=\"caption-attachment-1511\" class=\"wp-caption-text\">Dont use the database in views<\/p><\/div>\n<p>After than we run NDepend. It will discover violations of our rules and display it as warning:<\/p>\n<div id=\"attachment_1512\" style=\"width: 657px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/oh-a-warning.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1512\" class=\"size-full wp-image-1512\" title=\"oh-a-warning\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/oh-a-warning.png\" alt=\"Warning about the violations\" width=\"647\" height=\"99\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/oh-a-warning.png 647w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/oh-a-warning-300x45.png 300w\" sizes=\"(max-width: 647px) 100vw, 647px\" \/><\/a><p id=\"caption-attachment-1512\" class=\"wp-caption-text\">Warning about the violations&nbsp;<\/p><\/div>\n<p>And of course tell you the exact code location which violates the rule:<\/p>\n<div id=\"attachment_1515\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/ndepend-found-it.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1515\" class=\"size-medium wp-image-1515\" title=\"ndepend-found-it\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/ndepend-found-it-300x71.png\" alt=\"Ndepend found it\" width=\"300\" height=\"71\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/ndepend-found-it-300x71.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/03\/ndepend-found-it.png 720w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1515\" class=\"wp-caption-text\">NDepend found it<\/p><\/div>\n<h2>Conclusion And Follow-Up<\/h2>\n<p>We\u2019ve seen that we can use NDepend to write rules which enforce certain dependency rules. This allows us to spot quickly problematic usage of classes and interfaces in problematic places.<\/p>\n<p>Next time I\u2019ll try to integrate NDepend in my favorite continues build tool, <a href=\"http:\/\/www.jetbrains.com\/teamcity\/\">TeamCity<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time I gave a short overview over the features of NDepend. We know that NDepend can show a detailed dependency analysis your projects.&nbsp; This time I show you how&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[126,19],"tags":[21,211],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1507"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1507"}],"version-history":[{"count":10,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1507\/revisions"}],"predecessor-version":[{"id":3976,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1507\/revisions\/3976"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}