magic GET and SET for object initialization question
I would like to know the best practice for a class oriented DDD.
Since i am doing domain validation in custom setters named ChangeX(string x) i might be pushed to use this as property.
public virtual string example { get; private set; }
However, that not very good since it disable me from using the object initialization feature such as :
new Object { Example = "Some example" }
So i though why not passing the custom set into the property set ? like this
public virtual string Example { get { return Example; } set { ChangeExample(value); } }
Is this c开发者_运维百科an lead to any problems ? it is against best practices ?
Thanks.
Real problem here is using setters as such. Why do You need them?
When You use setters, You lose isolation - You can modify state of objects from outside w/o them knowing that. That leads to procedural code.
In contrast - You should ask objects to do something (not just modify their state) that would eventually might lead to them changing their own state.
I think this solution is fine. One reason to have setters is to make sure your under laying fields never hold incorrect values.
精彩评论