In the context of this post by Igor Minar, lead of AngularJS:
MVC vs MVVM vs MVP. What a controversial topic that many developers
I believe Igor's take on this, as seen in the quote you have provided, is just the iceberg tip of a far greater problem.
MVC and its derivatives (MVP, PM, MVVM) are all good and dandy within a single agent, but a server-client architecture is for all purposes a two-agent system, and people are often so obsessed with these patterns that they forget that the problem at hand is far more complex. By trying to adhere to these principles they actually end up with a flawed architecture.
Let's do this bit by bit.
Within Angular context, the view is the DOM. The guidelines are:
Do:
Don't:
As tempting, short, and harmless this looks:
ng-click="collapsed = !collapsed"
It pretty much signify any developer that now to understand how the system work they need to inspect both the Javascript files, and the HTML ones.
Do:
Don't:
The reason for the last guideline is that controllers are sisters to views, not entities; nor they are reusable.
You could argue that directives are reusable, but directives too are sisters to views (DOM) - they were never intended to correspond to entities.
Sure, sometimes views represent entities, but that's a rather specific case.
In other words, controllers shall focus on presentation - if you throw business logic in, not only you are likely to end up with an inflated, little-manageable controller, but you also violate the separation of concern principle.
As such, controllers in Angular are really more of Presentation Model or MVVM.
And so, if controllers shouldn't deal with business logic, who should?
Unless you are writing an offline web application, or an application that is terribly simple (few entities), you client model is highly likely to be:
In traditional MCV, the model is the only thing being persisted. Whenever we talk about models, these must be persisted at some point. Your client may manipulate models at will, but until the roundtrip to the server was completed successfully, the job ain't done.
The two points above should serve as a caution - the model your client holds can only involve a partial, mostly simple business logic.
As such, it is perhaps wise, within client context, to use lowercase M
- so it's really mVC, mVP, and mVVm. The big M
is for the server.
Perhaps one of the most important concepts about business models is that you can subdivide them to 2 types (I omit the third view-business one as that's a story for another day):
firstName
and sirName
properties, a getter like getFullName()
can be considered application-independent.It is important to stress that both of these within a client context are not 'real' business logic - they only deal with the portion of it that is important for the client. Application logic (not domain logic) should have the responsibility of facilitating communication with the server and most user interaction; while the domain logic is largely small-scale, entity-specific, and presentation-driven.
The question still remains - where do you throw them within an angular application?
All these MVW frameworks use 3 layers:
But there are two fundamental issues with this when it comes to clients:
An alternative to this strategy is the 4 layer strategy:
The real deal here is the application business rules layer (Use cases), which often goes amiss on clients.
This layer is realised by interactors (Uncle Bob), which is pretty much what Martin Fowler calls an operation script service layer.
Consider the following web application:
A few things should happen now:
Where do we throw all of this?
If your architecture involves a controller that calls $resource
, all of this will happen within the controller. But there is a better strategy.
The following diagram shows how the problem above can be solve by adding another application logic layer in Angular clients:
So we add a layer between controller to $resource, this layer (lets call it interactor):
UserInteractor
.And so, with the requirements of the concrete example above:
validate()
validate()
method.createUser()