I have an object called SecurityServer.

It is created using SecurityServerFactory.

I need to serialize SecurityServer to disk/hardware token/ or some other storage.

Which is more correct:

  • securityServerInstance.Save()
  • StorageAbstract.SaveServer(securityServerInstance);
  • Both?
  • Neither? Use injection?

I have a feeling I need to understand domain driven design, but I'm self taught at this point and am starting to finally "get it"


3 Answers 3


If you use StorageAbstract.SaveServer(securityServerInstance); then StorageAbstract will need to be able to access everything inside a SecurityServer object that needs to be serialized. This could be a problem if there is internal state that needs to be saved but is not - and possibly should not be - accessible outside the of SecurityServer. If you don't want to worry about that, then maybe have the object save itself.

  • Excellent point. Security server has a private key, so I'll do Instance.Save to serialize it Feb 24, 2017 at 20:28

In the context of DDD (Domain Driven Design), the only consistent answer is to use a "Repository": rep.save(securityServerInstance), where rep is an instance of SecurityServerRepository.

  • So should I use one object to create the server and the repository to load and save it? Any way I can still keep the private key private scope within the server? Feb 24, 2017 at 20:50
  • I don't know whether you actually need a SecurityServerFactory. In DDD, a "Factory" isn't meant to simply instantiate ("new") an object, but to assemble a reasonably complex aggregate composed of multiple related entities. And a "Repository" is used to persist and reconstitute whole aggregates. Each Repository implementation does need to have read/write access to each and every data item of the entity types it's responsible for. Whether that implies the need for a non-private "getter"/"setter" in the entity class, depends on the underlying persistence technology being used.
    – Rogério
    Feb 24, 2017 at 21:00

You were are faced with two choices: 1. Expose private members so an outside class can access those members 2. Make the object save itself

I suggest a third option: Use the visitor design pattern and create a visitor whose purpose is to serialize the object.

This helps your Security server have less responsibilities.

Think of another example, such as making a textual report from the class. This report needs to include data only visible to the security server. Would you also like to implement that method within the security server?

Using the visitor design pattern would help you to reduce code within the class and separate responsibilities

  • 2
    How would the visitor access the private members? Feb 25, 2017 at 11:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.