<p>The <a href="http://en.wikipedia.org/wiki/Law_Of_Demeter" rel="noreferrer">wikipedia article</a> about <a href="http://c2.com/cgi/wiki?LawOfDemeter" rel="noreferrer">Law of Demeter</a> says:</p>

<blockquote>
  <p>The law can be stated simply as "use only one dot".</p>
</blockquote>

<p>However a <a href="http://weblogs.asp.net/jgalloway/archive/2006/12/06/a-simple-example-of-a-fluent-interface.aspx" rel="noreferrer">simple example</a> of a <a href="http://en.wikipedia.org/wiki/Fluent_interface" rel="noreferrer">fluent interface</a> may look like this:</p>

<pre><code>static void Main(string[] args)
{
   new ZRLabs.Yael.Pipeline("cat.jpg")
        .Rotate(90)
        .Watermark("Monkey")
        .RoundCorners(100, Color.Bisque)
        .Save("test.png");
}
</code></pre>

<p>So does this goes together?</p>

[{'answer_id': 67593, 'author': 'Quibblesome', 'author_id': 1143, 'author_profile': 'https://Stackoverflow.com/users/1143', 'pm_score': 3, 'selected': False, 'text': '<p>Yes, although you have to apply some pragmatism to the situation. I always take the Law of Demeter as a guideline as opposed to a rule.</p>\n\n<p>Certainly you may well want to avoid the following:</p>\n\n<pre><code>CurrentCustomer.Orders[0].Manufacturer.Address.Email(text);\n</code></pre>\n\n<p>perhaps replace with:</p>\n\n<pre><code>CurrentCustomer.Orders[0].EmailManufacturer(text);\n</code></pre>\n\n<p>As more of us use ORM which generally presents the entire domain as an object graph it might be an idea to define acceptable "scope" for a particular object. Perhaps we should take the law of demeter to suggest that you shouldn\'t map the entire graph as reachable.</p>\n'}, {'answer_id': 67605, 'author': 'Sebastian Rittau', 'author_id': 7779, 'author_profile': 'https://Stackoverflow.com/users/7779', 'pm_score': 8, 'selected': True, 'text': '<p>Well, the short definition of the law shortens it too much. The real "law" (in reality advice on good API design) basically says: Only access objects you created yourself, or were passed to you as an argument. Do not access objects indirectly through other objects. Methods of fluent interfaces often return the object itself, so they don\'t violate the law, if you use the object again. Other methods create objects for you, so there\'s no violation either.</p>\n\n<p>Also note that the "law" is only a best practices advice for "classical" APIs. Fluent interfaces are a completely different approach to API design and can\'t be evaluated with the Law of Demeter.</p>\n'}, {'answer_id': 67615, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 5, 'selected': False, 'text': '<p>Not necessarily. "Only use one dot" is an inaccurate summary of the Law of Demeter.</p>\n\n<p>The Law of Demeter discourages the use of multiple dots when each dot represents the result of a different object, e.g.:</p>\n\n<ul>\n<li>First dot is a method called from ObjectA, returning an object of type ObjectB</li>\n<li>Next dot is a method only available in ObjectB, returning an object of type ObjectC</li>\n<li>Next dot is a property available only in ObjectC</li>\n<li>ad infinitum</li>\n</ul>\n\n<p>However, at least in my opinion, the Law of Demeter is not violated if the return object of each dot is still the same type as the original caller:</p>\n\n<pre><code>var List&lt;SomeObj&gt; list = new List&lt;SomeObj&gt;();\n//initialize data here\nreturn list.FindAll( i =&gt; i == someValue ).Sort( i1, i2 =&gt; i2 &gt; i1).ToArray();\n</code></pre>\n\n<p>In the above example, both FindAll() and Sort() return the same type of object as the original list. The Law of Demeter is not violated: the list only talked to its immediate friends.</p>\n\n<p>That being said <em>not all</em> fluent interfaces violate the Law of Demeter, just as long as they return the same type as their caller.</p>\n'}, {'answer_id': 67641, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 3, 'selected': False, 'text': "<p>The spirit of Demeter's Law is that, given an object reference or class, you should avoid accessing the properties of a class that's more than one sub-property or method away since that will tightly couple the two classes, which might be unintended and can cause maintainability problems.</p>\n\n<p>Fluent interfaces are an acceptable exception to the law since they're <strong>meant</strong> to be at least somewhat tightly coupled as all the properties and methods are the terms of a mini-language that are composed together to form functional sentences.</p>\n"}, {'answer_id': 2482579, 'author': 'Peter Perháč', 'author_id': 81520, 'author_profile': 'https://Stackoverflow.com/users/81520', 'pm_score': 1, 'selected': False, 'text': "<p>There's no problem with your example. After all, you're rotating, watermarking, etc... always the same image. I believe you're talking to a Pipeline object all the while, so as long as your code only depends on the class of the Pipeline, you're not violating LoD.</p>\n"}, {'answer_id': 4269333, 'author': 'Andrei Rînea', 'author_id': 1796, 'author_profile': 'https://Stackoverflow.com/users/1796', 'pm_score': 3, 'selected': False, 'text': '<p>1) It does not violate it at all.</p>\n\n<p>The code is equivalent to </p>\n\n<pre><code>var a = new ZRLabs.Yael.Pipeline("cat.jpg");\na = a.Rotate(90);\na = a.Watermark("Monkey");\na = a.RoundCorners(100, Color.Bisque);\na = a.Save("test.png");\n</code></pre>\n\n<p>2) As Good Ol\' Phil Haack says : <a href="http://haacked.com/archive/2009/07/14/law-of-demeter-dot-counting.aspx" rel="noreferrer">The Law of Demeter Is Not A Dot Counting Exercise</a></p>\n'}, {'answer_id': 62480459, 'author': 'fruqi', 'author_id': 1110171, 'author_profile': 'https://Stackoverflow.com/users/1110171', 'pm_score': 1, 'selected': False, 'text': "<p>At heart, an object shouldn't expose its internals (data) but rather expose functions to operate with the internals.</p>\n\n<p>Taking that into account, Fluent API is asking the object to work on something with its data, not asking its data.</p>\n\n<p>And that doesn't violate any of the Laws of Demeter.</p>\n"}]
['https://Stackoverflow.com/questions/67561', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2361/']