I have traditionally been a developer of desktop applications, but circumstances have driven me to make the web client and the corresponding REST API logic for a project in which I am involved. Unfortunately, I am a one-man show, so my opportunities to learn new models or techniques from colleagues are somewhat limited. When I was ramping up, I had the opportunity to work (briefly) with an entrepreneur who exposed me to the idea that my server REST logic should be separated into one Controller (where the actual GET / PUT / POST / DELETE methods live) and a A service who does most of the work. As I was told, the A service could still interact with one or more Suppliers or Stores.
My understanding is that a Provider would encapsulate the logic that interacts with another system, maybe another web API, or a little weird legacy code, or maybe a little bit of proprietary hardware (like a temperature gauge, for example). In addition, a Shop would pack CRUD logic for real data objects in SQL, NoSQL, text files, whatever.
Assuming all of this makes sense, and this is actually the way the pros do it, he further advised me to incorporate the denomination into my lessons, like this:
PizzaController can proxy received web API calls to the PizzaService, who in turn could speak to both PizzaProvider and the RefridgeratorStore.
I'm not 100% positive, that's how the real world does things – but it made enough sense for me to appear believable and I have generally adopted this model and up to now it has worked well enough to organize my logic.
But this is where some questions arise:
First, is this vision of class separation really the way others structure their code? And if I am close, but not quite, what corrections should I make?
Second, is it legitimate for a A service to instance and take advantage of a second A service? For example, if my PizzaService must decide if we want delivery or if we're going to make pizza from scratch – he may want to invoke the PizzaProvider -or- he might just want to defer to the PizzaMakerService. If the PizzaService does not make this decision, so the logic of decision should live earlier in the food chain (no pun intended). It would infer my PizzaController will have to decide whether to use the PizzaService -or there PizzaMakerService; and it doesn't feel right to me.
And finally, (following the model that was shown to me) my Services frequently return a data object to my Controller, where the Controller will map one or more properties to a ViewModel who returned to my client. I discovered that I could just as easily map the relevant data bits into an anonymous object (C #) on the fly and return it to my client. The JSON returned is the same, so why introduce the class definition for a ViewModel at all? Is there a taboo against creating an anonymous object in the Controller and return it?
I realize (in my situation) that I can pretty much do whatever I want – how I name the classes, how I separate the logic, if I use anonymous objects – it's really entirely my code. But these questions have been pestering me for quite some time and I would like to make things as close as possible to "properly". It is likely that these questions (or a variation) have been asked and answered before, so I apologize now for any duplication – but for the life of me, I can't seem to find direct answers.