|
|
|
|
|
by aidos
4695 days ago
|
|
Is it not ok to have all of that state you're talking about down in the service? In my experience any state you have in the controllers can be problematic in bigger apps. When I started out I used to proxy everything through the controller but I've found that you end up with a lot of duplication that doesn't ever seem to pay off. When you're talking about the various auth states you might have - they could be changed by more than just this view. That state could also be exposed in different controllers / views in different ways. Sort of seems like the state that each of those would use is the same (that in the service) - so is it ok for the views to just reference that state in the service directly? Interested in hearing where that falls down. I'm fairly new to developing rich interfaces and I've struggled to find completely deinitive information on how best to architect these patterns. Lots of examples out there bind the views to the services but that may be shortsighted. |
|
Specifically, you should be following the SRP - duplicated code is a symptom that you are not. Without seeing your code that you feel doesn't pay off, it's difficult to answer your question more directly.
I will say there's no reason you can't design your server-side services to be consumed directly by the view, in which case I wouldn't see a problem referencing the service directly from the view (e.g. the UserService exposed by the server implements (a) normal login, (b) login as part of checkout and (c) registration).
Alternatively, you could build a client-side service to wrap the server-side service and reference that directly from your views (e.g. a UserController that wraps the UserService and is consumed by (a) the omnipresent login view, (b) a login/signup view, and (c) the registration view).
Over and above this, there is some middle-ground where you reuse structures. For example, a User structure (with IsAuthenticated, UserName, FullName etc.) would be assigned to a $scope.CurrentUser property for the views. Consequently you don't reimplement the User object the service implements, and you don't vaguely depend on the entire UserService, but the data contract the view does depend on from the controller is still explicit by looking at the $scope definition in the controller. Similarly, the operation contract is defined by the controller functions. This improves maintainability and readability IMO.