1

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();
    }
}
Jin-Wook Chung
  • 4,196
  • 1
  • 26
  • 45

1 Answers1

2

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.

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • Thanks for your answer. Could I have some more question? 1) Could you give me the more information about "except if selectConstructorInfois only ever used ...". This means the method only return value(or object) as the specified return type without having logic or operation. – Jin-Wook Chung May 08 '12 at 09:58
  • Sorry, it's a bit long. 2) As far as I understand, keeping up the LoD principle means that a method should be return only simple value(int, string so on) or data-structure value like DTO(data transfer object), because any method of the returned value should not be used on chained state. – Jin-Wook Chung May 08 '12 at 10:05
  • @jwjung Any object has behaviour but is also a value. In your case if you compare the selectedConstructorInfo with another object you are using it for it's value. If you construct a new object using the Invoke method you are now relying on behaviour of a friends friend which violates LoD – Rune FS May 08 '12 at 10:07
  • I see what you mean. The `var testClass = new TestClass(selectConstructorInfo);` violates the LoD though, but if I use like `new TestClass(concreteSelector);`, it means the latter is not the case of the violation. (for the name of parameters, refer to the above). – Jin-Wook Chung May 08 '12 at 10:22