This is not about whether or not getters/setters are wrong. I understand its impact to encapsulation and that question has been raised here and SO several times already. I also do not want to just ask for an example of how to change object state as all the examples I've seen are so naive that they're only practical for simple objects. I do want to ask for an example on more specific terms so that I can make the rubber meet the road in a very practical way concerning this concept.

The Question

How can an object's state can be changed when one of its data objects cannot be initialized upon construction of the parent object to begin with?

More Specifically

Even more specifically, I have this situation where I am defining a domain entity for an API that is representative of a data entity in a third-party system whose data is stored in files. (For this question I'm using domain/data entity to distinguish the difference between the two). This is a requirement to ensure loose coupling between layers and the fact that though the entities are representative of each other, they are not one-to-one representations, more representative in principle. The client code will be able to create these domain entities and commit an internal transaction designed to coordinate the write to the files. One of the domain entity's properties is a Handle object that contains the third-party data entity's handle ID so that any later changes to the data entity in the file can be easily and quickly coordinated. This handle ID can't be set to the domain entity until it captures it in the data access layer on the first write.

In a nutshell

So in a scenario where the object cannot obtain all its state on construction, but get the rest at a different point in the program (and maybe even a totally different application layer), how should this look without breaking principles of encapsulation and Tell-Don't-Ask?

Btw... I'm using c#, so any comparable example along these lines would help me better relate.

  • The only way you can create an object and hand it some state later is to... create an object, and hand it some state later. You can do that by calling a method or setting some properties on the object after instantiation. – Robert Harvey Feb 10 '16 at 21:53
  • Please note that asking for an example is off-topic. I reworded your question to avoid the "resource request" verbiage. – user22815 Feb 10 '16 at 21:55
  • What exactly you are asking? You can assign the handle by defining a method such as public void AssignHandleID(int idOfHandle). Does that not solve your problem? – Andy Feb 10 '16 at 22:28
  • 1
    You're basically asking us how to let the client change the state of the object without allowing the client to change the state of the object. – Robert Harvey Feb 11 '16 at 1:51
  • 1
    Have a look at Data Transfer Objects, or Fowler's Anemic Domain Model article. Even Fowler admits that sometimes you have to pass around an open data object. Encapsulation and "Tell, don't ask" are just principles, not laws. – Robert Harvey Feb 11 '16 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.