title: How to solve the violations of the Law of Demeter?
body: <p>A colleague and I designed a system for our customer, and in our opinion we created a nice clean design. But I'm having problems with some coupling we've introduced. I could try to create an example design which includes the same problems as our design, but if you forgive me I'll create an extract of our design to support the question.</p>
  
  <p>We're developing a system for the registration of certain treatments for a patients. To avoid having a broken link to image I'll describe the conceptual UML class diagram as a c# style class definition.</p>
  
  <pre><code>class Discipline {}
  class ProtocolKind 
  { 
     Discipline; 
  }
  class Protocol
  {
     ProtocolKind;
     ProtocolMedication; //1..*
  }
  class ProtocolMedication
  {
     Medicine;
  }
  class Medicine
  {
     AdministrationRoute;
  }
  class AdministrationRoute {}
  </code></pre>
  
  <p>I'll try to explain a bit about the design, a protocol is the template for a new treatment. And a protocol is of a certain Kind and has medications which need to be administered. Per protocol the dosage can differ for the same medicine (amongst other things), so that's stored in the ProtocolMedication class. AdministrationRoute is the way the medicine is administered and iscreated/updated separately from the protocol management.</p>
  
  <p>I've found the following places which we'll have a violation of the Law of Demeter:</p>
  
  <h2>Violations of the Law of Demeter</h2>
  
  <h3>Inside of the BLL</h3>
  
  <p>For example, inside the business logic of the ProtocolMedication there are rules which depend on the AdministrationRoute.Soluble property of the medicine. The code would become</p>
  
  <pre><code>if (!Medicine.AdministrationRoute.Soluble)
  {
     //validate constrains on fields
  }
  </code></pre>
  
  <h3>Inside the repositories</h3>
  
  <p>A method which would list all the protocols in a certain Discipline would be written as:</p>
  
  <pre><code>public IQueryable&lt;Protocol&gt; ListQueryable(Discipline discipline)
  {
      return ListQueryable().Where(p =&gt; (p.Kind.Discipline.Id == discipline.Id)); // Entity Frameworks needs you to compare the Id...
  }
  </code></pre>
  
  <h3>Inside the User Interface</h3>
  
  <p>We use ASP.NET (no MVC) for the interface of our system, in my opinion this layer currently has the worst violations. The databinding of a gridview (a column which must display the Discipline of a protocol must bind to Kind.Discipline.Name), which are strings, <strong>so no compile time errors</strong>.</p>
  
  <pre><code>&lt;asp:TemplateField HeaderText="Discipline" SortExpression="Kind.Discipline.Name"&gt;
     &lt;ItemTemplate&gt;
        &lt;%# Eval("Kind.Discipline.Name")%&gt;
     &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p>So I think the actual question might be, when would it be okay to look at it more as the Suggestion of Demeter, and what can be done to solve the violations of the Law of Demeter?</p>
  
  <p><em>I've got a few idea's of myself but I'll post them as answers so they can be commented and voted on seperatly. (I'm not sure this is the SO way to do it, if not, I'll delete my answers and add them to the question).</em></p>