mardi 11 juin 2019

Models per every layer in web api - is it really required?

We're building Web API. In our app we're mostly using 3 layers:

API (controllers etc.)
Business (services)
Infrasctructure (repositories etc.)

All these layers got models respectively:

VieModels
DTOs
Models

I'm trying to convince my friend, that having DTOs is completely useless...Our DTOs are almost identical to Models... So far, lets say I want to create controller returning Customer, what the flow looks like backward:

  1. CustomerRepository returns CustomerModel
  2. CustomerService invoke CustomerRepository and maps CustomerModel to CustomerDTO
  3. CustomerController invoke CustomerService and maps CustomerDto to CustomerViewModel.

If we just skip those DTO's we would have much less models layers, mappings etc. but my friend keeps talking that people write everywhere that layers can only talk with their models...

I don't see any problem for Controller invoking directly CustomerRepo if there is no any calculation required for service in this particular flow and just make one mapping from CustomerModel to CustomerViewModel. Either when calucation is required just skip DTOs, CustomerService just gets CustomerModel from repo, calculcate something with it and returns CustomerModel to controller.

Is it really required/good practice to have DTOs (that are almost the same Models are), and just keep mapping those Models to DTOs, because controller should only use DTOs created in business?

Aucun commentaire:

Enregistrer un commentaire