A discussion came up in the office today whether or not it is a OOP convention to use this:
private string myField;
public string MyProperty {
get { return myField; }
}
public MyClass(string field) {
myField = field;
}
Over this:
public string MyProperty {
get;
private set;
}
public MyClass(string field) {
MyProperty = field;
}
During Domain Driven Development.
A lot of the DDD examples we've looked at have the code laid out using the 1st approach above, but never the second. I know several people in our office (myself included) have a personal preference to the 2nd approach for various reasons, while others prefer the 1st, or don't care either which way which approach gets used.
Currently, our code is mixed between the 2 different approaches with some classes using approach #1, others use #2, and others use a combination of both (one property might have public get/private set while another only has a public get that returns a private field).
Basically, we're wondering what approach is best fit for DDD OOP best practices and the reasons why as we're due for a code spring clean, and tidying up the domain internals is one of the items on the list.