5

I'm developing a back-end application and I ran into the problem of too many conversions between layers. For a single type it is like this:

enter image description here

So every time I update the model, I need to update six classes and several converters.

How to tackle this complexity or maybe I shouldn't?

EDIT: On the second thought, after some more development, when an amount of different logic code will grow and models will become more stable this problem might largely go away.

3
  • Your second thought is not what I observe in reality. For most systems I have seen, growing means alway growing both, the underlying data model as well as the functionality on top of that model, so the problem does not "go away". But for a larger model, it pays off to invest some more time, effort and maybe money to build some infrastructure to mitigate the issue. – Doc Brown Feb 20 at 21:27
  • @DocBrown what kind of infrastructure can mitigate this problem? I believe this can be a good answer. – Yola Feb 20 at 21:34
  • 2
    Frameworks, generic object mappers, code generators - things already mentioned in Alexander Petrov's answer. – Doc Brown Feb 20 at 22:20
5

This complexity may not be that big in general depending on the application and the complexity of your business case.

If we presume that:

  1. Your entities are your persistence layer - understand hibernate/jooql or whatever.
  2. You business logic is in some domain objects, for example, agnostic of the persistence.
  3. On top, you have your DTOs presentation layer.

It looks alright for a complex enough application.

If your case is simple, it may be possible to merge some of these layers, e.g., your persistence objects may as well be your domain objects, but that depends very much on the case. For a very simple case, it may well be justified to just merge everything.

With regards to how you can reduce some of the boilerplate code, there are plenty of frameworks that can help you. If it was Java, I would have pointed Mapstructs out to you. It is a Mapper generation library that will make your conversion between layers quite straight forward. Other approaches are also possible.

Also layer number 2 can be represented as simple services. The usage of Domain Objects is not mandatory.

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.