Traditionally I am a developer of desktop applications, but circumstances have led me to the function of making a web client and the corresponding REST api logic for a project I am involved in. Unfortunately, I am a one-man show, so my opportunities to learn co-workers' new patterns or techniques are somewhat limited. When it was increasing, I had the opportunity to work (briefly) with a contractor who exposed me to the idea that my server's REST logic should be separated into a Controller (where the actual GET / PUT / POST / DELETE methods live) and a Service that does the heavy lifting. As explained to me, the Service could further interact with one or more Providers or stories.
I understand that a Provider It would wrap logic that interacts with another system, maybe another web API, or some legacy code, or maybe some proprietary hardware bit (like a temperature gauge, for example). Also, a Store It would wrap the CRUD logic for real data objects to SQL, NoSQL, text files, whatever.
Assuming all of this makes sense, and it's actually how professionals do it, he advised me to incorporate the name into my classes, like so:
PizzaController could send the received web API calls to Pizza service, who in turn could talk so much with him Pizza supplier and the RefridgeratorStore.
I'm not 100% sure this is how the real world does things, but it made enough sense for me to appear credible and overall I've adopted this pattern and so far it has worked well enough to organize my logic.
But this is where some questions come in:
First, is this vision of separation of my classes really how others structure their code? And if I am close, but not entirely, what corrections should I make?
Second, is it legitimate for you? Service for example and take a second Service? For example, what if my Pizza service you have to decide if we want delivery or if we are going to make a pizza from scratch you may want to invoke Pizza supplier -or- you may simply want to defer to PizzaMakerService. If he Pizza service does not make this decision, so the decision logic would have to live in the food chain first (no pun intended). That would infer my PizzaController would have to decide whether to use the Pizza service -wave PizzaMakerService; and that doesn't smell good to me.
And finally, (following the pattern shown to me) my Services frequently return a data object to my Controller, where he Controller will assign one or more properties in a ViewModel that is returned to my client. I found that I could easily map the relevant data bits into an anonymous object (C #) on the fly and return it to my client. The returned JSON is the same, so why enter the class definition for a ViewModel absolutely? Is there a taboo against simply creating an anonymous object in the Controller and giving it back?
I realize (in my situation) that I can do almost anything I want: how do I name the classes, how do I separate the logic, if I use anonymous objects, is it really my code. But these questions have been bothering me for a while, and I'd like to be doing things as close as possible & # 39; correctly & # 39 ;. These questions (or a variation) are likely to have been asked and answered before, so I will apologize now for any duplication, but for the life of me, I can't seem to find any direct answers.