Models and Services
Best practice question:
If a service class loads an xml file that is destined for the
model, should you inject the into the service? Or is it better form
to dipatch an event that results in a command etc.
Comments are currently closed for this discussion. You can start a new one.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
1 Posted by John R. Nyquist on Sep 19, 2012 @ 08:03 PM
Looking at the RL book examples, it looks like injecting the model into the service is an accepted practice.
Support Staff 2 Posted by Ondina D.F. on Sep 21, 2012 @ 01:28 PM
Hi John,
Of course!
In case you’re aiming for extreme decoupling and separation of concerns, you won’t inject the model into the service. Instead you inject it into the command triggered by the event dispatched by the service when it’s done loading data. The command will then access model’s API.
Also, if you want a service to be generic, or if more than one model should be populated with service’s results, then it’s better if commands take care of updating the model(s).
On a scale of 1 to 10, I’d give Adobe’s approach a 1, and the following à la Robotlegs MVCS a 10:
View -> ViewEvent (UI interaction) -> custom Event ->Mediator -> custom Event re-dispatched on a shared event dispatcher -> Command -> Service’s API -> ServiceEvent -> custom Event dispatched on a shared event dispatcher -> Command->Model’s API-> custom Event dispatched on a shared event dispatcher -> Mediator ->event’s payload-> View’s API
There are obviously other possible and valid combinations between the two extremes.
The level of decoupling, cohesion, dependency, or encapsulation of classes in a project
depends very much on the use case, on project’s requirements, project type (game, enterprise, mobile), size of the project, team, on personal preferences…
In other words, the specificity of an individual project may require an adaptation of, or deviation from the Best Practices. If strictly following the Best Practices guidelines feels like hammering square pegs into round holes, then either you’re not familiar enough with the concepts yet, or it’s probably time to find a compromise, a middle way that best suits your project. Also, RL’s Best Practices revolve around MVCS, but maybe your project would benefit more from adopting another design patterns (say, MVP).
I’m saying all this because the Best Practices and/or some of the answers on this forum may sound a bit restrictive or prescriptive to someone new to rl. Or, on the contrary, many people are surprised to see different approaches presented in form of answers or demos, and don’t know what direction to take. I think having more options to choose from is a good thing, even if it’s a bit of a challenge for beginners.
My advice is to get familiar with the best practices and the extreme, “pure” MVCS scenarios first, while keeping in mind that they are not the ‘Ultimate Answer’, and that Robotlegs, as a framework, is really very flexible and permissive :)
Cheers,
Ondina
3 Posted by John R. Nyquist on Sep 21, 2012 @ 02:42 PM
I've been doing OO for a looong time, so I was building this setup: View -> ViewEvent (UI interaction) -> custom Event ->Mediator -> custom Event re-dispatched on a shared event dispatcher -> Command -> Service’s API -> ServiceEvent -> custom Event dispatched on a shared event dispatcher -> Command->Model’s API-> custom Event dispatched on a shared event dispatcher -> Mediator ->event’s payload-> View’s API
That's when I thought I'd post the question, after which I saw the book examples that coupled the model to the service. I agree there are a lot of factors in deciding how pure you can (or want to) go on each particular project. It's nice to hear how others are working with RL.
Support Staff 4 Posted by Ondina D.F. on Sep 21, 2012 @ 03:21 PM
Cool! Then you’ll become an RL expert in no time:)
I agree. Have you seen the long list
of examples and tutorials written by different rl users? A wide range of rl-coding styles.
If you don’t have any other questions or any other comments to add, you can close the discussion. („Close the discussion“ button on the right side of the page)
5 Posted by John R. Nyquist on Sep 21, 2012 @ 03:56 PM
I had not been to that page yet, thanks!
John R. Nyquist closed this discussion on Sep 21, 2012 @ 03:57 PM.