Object-oriented – Best practices for serialization of DDD aggregates

domain-driven-designobject-orientedormserialization

According to DDD domain logic should not be polluted with technical concerns like serialization, object-relational mapping, etc.

So how do you serialize or map the aggregates' state without publicly exposing it via getters and setters? I have seen lots of examples for e.g. repository implementations, but practically all of them relied on public accessors on the entities and value objects for mapping.

We could use reflection to avoid public accessors, but IMO these domain objects would still implicitly be dependent on the serialization concern. E.g. you couldn't rename or remove a private field without tweaking your serialization/mapping configuration. So you have to consider serialization where you should instead be focussing on domain logic.

So what's the best compromise to follow here? Live with public accessors, but avoid using them for anything else but mapping code? Or did I just miss something obvious?

I'm explicitly interested in serializing the state of DDD domain objects (aggregates consisting of entities and value objects). This is NOT about serialization in general or transcation script scenarios where stateless services operate on simple data container objects.

Best Answer

Kinds of objects

For purposes of our discussion, let's separate our objects into three different kinds:

Business Domain logic

These are the objects that get work done. They move money from one checking account to another, fulfill orders, and all of the other actions that we expect business software to take.

Domain logic objects normally do not require accessors (getters and setters). Rather, you create the object by handing it dependencies through a constructor, and then manipulate the object through methods (tell, don't ask).

Data Transfer Objects

Data Transfer Objects are pure state; they don't contain any business logic. They will always have accessors. They may or may not have setters, depending on whether or not you're writing them in an immutable fashion. You will either set your fields in the constructor and their values will not change for the lifetime of the object, or your accessors will be read/write. In practice, these objects are typically mutable, so that a user can edit them.

View Model objects

View Model objects contain a displayable/editable data representation. They may contain business logic, usually confined to data validation. An example of a View Model object might be an InvoiceViewModel, containing a Customer object, an Invoice Header object, and Invoice Line Items. View Model objects always contain accessors.

So the only kind of object that will be "pure" in the sense that it doesn't contain field accessors will be the Domain Logic object. Serializing such an object saves its current "computational state," so that it can be retrieved later to complete processing. View Models and DTO's can be freely serialized, but in practice their data is normally saved to a database.

Serialization, dependencies and coupling

While it is true that serialization creates dependencies, in the sense that you have to deserialize to a compatible object, it does not necessarily follow that you have to change your serialization configuration. Good serialization mechanisms are general purpose; they don't care if you change the name of a property or member, so long as it can still map values to members. In practice, this only means that you must re-serialize the object instance to make the serialization representation (xml, json, whatever) compatible with your new object; no configuration changes to the serializer should be necessary.

It is true that objects should not be concerned with how they are serialized. You've already described one way such concerns can be decoupled from the domain classes: reflection. But the serializer should be concerned about how it serializes and deserializes objects; that, after all, is its function. The way you keep your objects decoupled from your serialization process is to make serialization a general-purpose function, able to work across all object types.

One of the things people get confused about is that decoupling has to occur in both directions. It does not; it only has to work in one direction. In practice, you can never decouple completely; there is always some coupling. The goal of loose coupling is to make code maintenance easier, not to remove all dependencies.

Related Topic