It's been given by Microsoft as a framework design guideline that properties should be independent of one another and not rely upon being set in any specific order.
Assume you have a triangle class that needs to support dimmensions and an area calculation. How would you model this?
This is of course the design that is considered gauche because Area is dependent on Base and Height being set first:
class Triangle{
public double Base {get;set;}
public double Height {get;set;}
public double Area {
get{
return (Base * Height) / 2;
}
}
}
Assuming you use a constructor, you can ensure defaults for this case but is this the correct approach?
class Triangle{
public Triangle(double b, double h){
Base = b;
Height = h;
}
public double Base {get;set;}
public double Height {get;set;}
public double Area {
get{
return (Base * Height) / 2;
}
}
}
You still have a property that has a dependency on other properties. In order to be a purist I can see only a few approaches (I guess they could be combined):
Make Base/Height have readonly members that can only be set in the constructor
Make the Area calculation into a method.
Use some kind of factory pattern + readonly members to ensure that although the dependency may exist, the values can only be set by the method that instantiates the Triangle class.
Questions:
Is the guideline practical (do you have to model a lot of complexity into your classes in order to support it)? [for example, a SqlConnection class allows you to initialize the connection string property but lets you change individual pieces of it such as command timeout]
How do you manage keeping your properties independent of each other?
Extra for people who use Silverlight / MVVM type architecture, do you accept dependencies in properties because of the way databinding works to a object? For example, binding a triangle instance that shows height, base and area on screen.