Two classes or One when maintaining Invariant and allowing De/Serialization

I am facing tough time deciding on whether to have two different classes in system, one for keeping class invariant and other for (de/)serialization or to just have a single class which does both.

The project I’m working on is in Java and for (de/)serialization I am using Jackson mappings.

Pros of Keeping 2 classes:

  • More In line with Single Responsibility Principle.
  • Better control on class invariant (Can make things Immutable, easier to have thread safe classes…)
  • (De/) Serialization does not involve any complex annotations, and works easily with all libraries that have Jackson integrated in them. (Have observed that AWS SWF SDK does not properly honor the annotations)

Cons of keeping 2 classes:

  • Violating Don’t Repeat Yourself principle, by having to keep the fields in the two classes and the to-and-fro conversion functions in sync.

What approach should I take here?

2

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *