I have a scenario in which an Entity defines an EntityRepresentation that is created from another end. The EntityRepresentation is a value object that implements some logic that should be performed on the other end after its transmission. In the end, the EntityRepresentation and Entity contains the same data but in the first case it is stored as a Python base type (string in my case), in fact the EntityMapper object is used to do the mapping between the two types in order to store the Entity mapped from the EntityRepresentation.

In other words, EntityRepresentation contains the same data as Entity but in a seriazable format that is determined by Entity. Since I'm trying to stick to a DDD I would know if what I'm doing is considered as anti-pattern. I'm a little skeptical about this design because I feel like I can stick with the Entity class only and the representation format could be handled by simply using a method on both ends. The fact is that this representation should not be stored along with the Entity props since it is subject to change in the future.

How correct would be let Entity provide a method to create a serializable representation of itself since my domain imposes a contraint on that?

Here is a sample code:

import json

class Entity:

    def __init__(self, id: str, name: str, type: str):
        self.id = id
        self.name = name
        self.type = type

class EntityRepresentation:

    def __init__(self, data: str = None):
        if data is None:
            d = {
                "id": "ID",
                "name": "NAME",
                "type": "TYPE"
            self.data = json.dumps(d)
            self.data = data

    def process(self):
        # performs ops on self.data
        return True

class EntityMapper:

    def fromRepr(self, repr: EntityRepresentation) -> Entity:
        return json.loads(repr.data, object_hook=lambda r: Entity(**r))

    def toRepr(self, ent: Entity) -> EntityRepresentation:
        return EntityRepresentation(json.dumps(ent.__dict__))

def main():

    mapper = EntityMapper()

    # the representation is created on the first end
    e_repr = EntityRepresentation()

    # serialization
    e_repr_json = json.dumps(e_repr.data)

    # the json is sent into a wire
    # it is then received and deserialized on the other end
    e_repr_rec: EntityRepresentation = json.loads(
        json.loads(e_repr_json), object_hook=lambda d: EntityRepresentation(json.dumps(d)))

    if e_repr_rec.process():

    return 0

if __name__ == '__main__':
  • Do you consider serialization and deserialization to be domain concerns?
    – Laiv
    May 31 at 20:35
  • yes (in my case) Jun 1 at 8:17

It would not be correct to have a method in the Entity to provide the representation. In DDD, the Entity should not care about the representations. You might need more than one representation for diferent use cases, and they should exist outside of the domain. For example, a service could use the entity to create the representation(s). The representation is only a detail.

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.