Pergunta

My question is about clean code or best practice. I have a big formGroup and in my opinion its more difficult to map the form to an object than to send the form to the backend.

best regards

Foi útil?

Solução

I expect from your question that its about having one entity model (that is used from the UI (Angular Component HTML) down through the Business logic, to the Data Access layer where its send in the same format to the backend), against having one model on the FE side (UI down till the data access layer) and then a different model for the communication with the backend.

Default answer: It depends. :-)

By directly sending the form to the backend, your UI and the backend gets tightly coupled.
That means if anything changes in the UI (for example split a value input into two, validate them seperatly), the backend will have to adept. And the other way round

On the other side, why adding a mapping layer, if you already know that the changes will always apply to client and server.

That means you have to look into the crystal ball and predict the future.

Personaly i try to be consistent.
That means if i have one place in my client application where i need mapping (and thats nearly always the case), then i generally add a mapping for all data.
As a result my frontend data entites don´t have to look the same as the entities i send to the backend.

Having my own FE entities, only loosely coupled to the backend, give me quite flexibility.

Mapping

  • PRO: Flexibility
  • CON: More code

People often add the argument that the mapping also results in a lower performance. In my experience its very rarely the case that the additional mapping has an impact on the user experience. So i would only consider that if its a lot of data that needs to get mapped or if i know that i have to support very slow devices.

Licenciado em: CC-BY-SA com atribuição
scroll top