Should domain objects implement IXmlSerializable?

I am creating a REST API that exposes data as XML. I have a whole group of domain classes in my domain layer that are intended for consumption by both the service level behind the API and the client API that we will provide to clients. (Clients have the ability to interact directly with the REST API, but the client API makes it easier). I want my domain classes to be clean from any data persistence logic, but I'm struggling to find out if it is normal for the domain classes to implement IXmlSerializable in order to simplify the process of serializing the data that is exposed and retrieved from the API. I began to think that I would save domain classes without any serialization logic and instead decorate them with serialization, for example. wrap the domain object inside the object that handles serialization. I make things more complicatedwhat should they be? Any thoughts on how I should approach this? Thank!

+3
source share
1 answer

Domain classes should only be associated with business logic, and not with persistence or serialization.

You need to create a set of Data Transfer Object (DTO) classes, each of which corresponds to one of the domain classes. These classes will only contain properties from the domain classes that you decide to open. This allows domain classes to have properties that are not displayed through your persistence or serialization levels.

DTO objects will be serialized and deserialized.

Then it will be convenient for you to create static "translate" methods for translation between the domain and DTO objects.

+5
source

Source: https://habr.com/ru/post/1724699/


All Articles