Suppose I have a Controller that takes a DTO that represents an Order. This DTO has inner DTOs that could potentially have entity IDs and/or entity data that's used to create an entity dynamically from that data, for example, an OrderProduct that has a price, qty, etc.
Before this order can be created, I need to run validation logic on the data, like "Does the store with the given ID exist?" and "Does an order with this externalId already exist?".
My question arises from the effort it takes to map a DTO to a model. The process of doing so requires the mapper to query for the models with the given IDs, query for models using external IDs (depending on inner DTOs). If an entity with such an ID exists, fetch it, if not, map it to a new model (this is the case for products, because for our use case, they're created dynamically if they aren't found).
In short, I'm faced with two scenarios. Map the DTO first and endure all those queries, only to find a simple check down the line fails because the storeId sent with the DTO didn't exist (could've prevented this before all the mapping by just asking if the store exists).
Or... run the necessary checks against the DTO and then map to entity. In other words, by running these validations using the IDs that I was given in the DTO before mapping it to a model, I prevent the unnecessary usage of database resources by only querying 2 times versus a dozen for each DTO with an ID that I need to map to a model.
So, what would you suggest is the correct course of action for this problem?
Would it be correct to validate the DTO IDs in the controller layer by calling a repository with the given IDs and also do the same validations in the service layer on the entity model? (the latter in case another service calls my service with that entity). Where I work, they seem to be very adamant about not injecting repositories into controllers.