Note that there are some explanatory texts on larger screens.

plurals
  1. POConfused about the law of Demeter principle
    primarykey
    data
    text
    <p>To explain my problem, let me show you the example code with C#.</p> <pre class="lang-cs prettyprint-override"><code>interface IConstructorInfoSelector { //ConstructorInfo is System.Reflection.ConstructorInfo class. ConstructorInfo SelectConstructorInfo(Type declaringType); } class TestClass { private readonly ConstructorInfo _constructorInfo; public TestClass(IConstructorInfoSelector constructorInfoSelector, Type type) { //Let the line to (A) _constructorInfo = constructorInfoSelector.SelectConstructorInfo(type); } public TestClass(ConstructorInfo constructorInfo) { _constructorInfo = constructorInfo; } public Type GetTypeForConstructor() { //Let the line to (B) return _constructorInfo.DeclaringType; } } </code></pre> <p>On the example, if I construct the TestClass with ctor(IConstructorInfoSelector, Type) and call the GetTypeForConstructor, it will violate the LoD(the law of Demeter principle) through the line (A) and (B).</p> <p>However, if I execute the following code, does the code violate the LoD? I think that on the one hand, it does not violate because the testClass object at the line (C) is initialized within a method and the GetTypeForConstructor method is called and on the other hand, it seem to violate the principle as the above case. To sum up, if a return object is used to create an other object, this execution will be considered as violation of the LoD?</p> <pre class="lang-cs prettyprint-override"><code>class LoDQuestionForTestClass { public void DeosThisVoliateTheLoD() { IConstructorInfoSelector concreteSelector = ...; Type testType = ...; var selectConstructorInfo = concreteSelector.SelectConstructorInfo(testType); //Let the line to (C) var testClass = new TestClass(selectConstructorInfo); var result = testClass.GetTypeForConstructor(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload