{"id":5244,"date":"2014-03-30T20:00:32","date_gmt":"2014-03-30T20:00:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/can-dependency-injection-prevent-a-circular-dependency-collection-of-common-programming-errors\/"},"modified":"2014-03-30T20:00:32","modified_gmt":"2014-03-30T20:00:32","slug":"can-dependency-injection-prevent-a-circular-dependency-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/can-dependency-injection-prevent-a-circular-dependency-collection-of-common-programming-errors\/","title":{"rendered":"Can dependency injection prevent a circular dependency?-Collection of common programming errors"},"content":{"rendered":"<p>You probably <em>could<\/em> solve this with DI, but you <em>shouldn&#8217;t<\/em>.<\/p>\n<p>If I understand correctly, you have something like this:<\/p>\n<pre>\n  + Assembly A           + Assembly B\n  |                      |\n  +-- Interface IFoo     +-- Class ConcreteFoo : IFoo\n  |                                   ^\n  +-- Class MyClass --&gt;-------&gt;-------|\n<\/pre>\n<p>In other words, you&#8217;re trying to get <code>MyClass<\/code> to reference <code>ConcreteFoo<\/code>, but you can&#8217;t because assembly <code>B<\/code>, which <code>ConcreteFoo<\/code> resides in, already depends on <code>IFoo<\/code> in <code>A<\/code>.<\/p>\n<p>This is a design error. If you declare the interface <code>IFoo<\/code> in Assembly <code>A<\/code>, but no concrete implementations, then any other interfaces\/classes in assembly <code>A<\/code> should <strong>only<\/strong> reference <code>IFoo<\/code>, <strong>never<\/strong> a concrete class that implements it.<\/p>\n<p>There are three ways to eliminate the circular dependency:<\/p>\n<ol>\n<li>\n<p>Make <code>MyClass<\/code> dependent on <code>IFoo<\/code> instead of <code>ConcreteFoo<\/code>. This is probably the best option if you can do it. If the issue is that you need a physical instance of <code>IFoo<\/code> for use in <code>MyClass<\/code> and don&#8217;t know where to get one from, then have it take an <code>IFoo<\/code> in the constructor &#8211; let whoever uses <code>MyClass<\/code> figure out what <code>IFoo<\/code> to use.<\/p>\n<\/li>\n<li>\n<p>Move the interfaces to their own assembly. This is still a reasonably good practice. Your design will look like this:<\/p>\n<\/li>\n<\/ol>\n<pre>  + Assembly App       + Assembly Interfaces      + Assembly Concrete\n      |                    |                          |\n      |                    +-- Interface IFoo         |\n      |                    |                  \\       |\n      +-- Class MyClass    |                   \\------+-- Class ConcreteFoo\n      |                    |                          |           ^\n      +---- Member Foo -&gt;---------------------&gt;-------------------|\n  <\/pre>\n<ol>\n<li>Move <code>MyClass<\/code> to its own assembly. Effectively your dependency tree will look the same as in #2 above, but if assembly <code>A<\/code> is much smaller than <code>B<\/code> then this would require less effort.<\/li>\n<\/ol>\n<p>Hope that helps.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You probably could solve this with DI, but you shouldn&#8217;t. If I understand correctly, you have something like this: + Assembly A + Assembly B | | +&#8211; Interface IFoo +&#8211; Class ConcreteFoo : IFoo | ^ +&#8211; Class MyClass &#8211;&gt;&#8212;&#8212;-&gt;&#8212;&#8212;-| In other words, you&#8217;re trying to get MyClass to reference ConcreteFoo, but you can&#8217;t [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5244","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5244"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5244\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}