Jin-Wook Chung Jin-Wook Chung - 8 days ago 5
C# Question

Confused about the law of Demeter principle

To explain my problem, let me show you the example code with C#.

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;
}
}


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).

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?

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();
}
}

Answer

If one object depends on the behavior of another object supplied by a third object you violate LoD. The populistic verion of which is "Don't trust a friend of a friend"

In your second example you have another object depending on an object supplied by a third party so yes that does violate "don't trust a friend of a friend" except if selectConstructorInfois only ever used for it's value.

It's worth to note that LoD was created for a specific project (Demeter) and that it in it's strictest form might not apply to any other project.