Posts Tagged ‘MVC

15
Nov
07

MVC, MVP

MVC

Controller: Observer, takes input events.
View: Observer, presents data.
Model: Observable, holds data and data logic.

Synopsis:
Both Controller and View are registering at the Model as Obeservers. When a Controller makes a change to the Model, the View is synchronized by call of Update()s. This round-trip happens every time some input or data change occured. Traditionally a Controller is the actual widget.

Relationship:
A View is disconnected from a Controller.
Both View and Controller know about Model.

Good points:
When data has multiple views, such as multiple windows showing different aspects of the same data (e.g. input window, graph window, statistics window), each View is responsible for presenting in its own way, with the controller isolated from this mess. “Conversation” passes through the Model, with the help of the Observer pattern. Thus, a harsh separation.

Bad Points:
The View is very thin: no direct way of saving a GUI state for example.

MVP

View: Takes input, provides presentation
Presenter: Decides what to do with input, decides how to provide output to the Presenter.
Model: Holds the data and data logic.

*note: this is the “Ruby MVC” actually. Be ware that Controller is now Presenter, by Ruby naming.

Synopsis:
When a View takes input, it triggers an appropriate event to the Presenter, which knows about the View and takes whatever it needs, in order to perform a Model query. The Model returns data to the Presenter, and the Presenter is now free to shape it to the View’s need, and update the View.
The View knows about the Presenter, the Presenter knows about the Model, and the Presenter and View are separated.
Sometimes the Presenter modifies the View too. Traditionally a View is the form with all the widgets, and the Model and Presenter are logical entities.

Yet another variation:
Another important point to make is the Passive View. Here, the View is totally disconnected from the Model, and the Presenter is responsible to fill/synchronize the data to it. This is useful for testing purposes. That is actually what is done with the “Ruby MVC”.

Or in other words:

Sometimes the layers are arranged so that the domain layer completely hides the data source from the presentation. More often, however, the presentation accesses the data store directly. While this is less pure, it tends to work better in practice. The presentation may interpret a command from the user, use the data source to pull the relevant data out of the database, and then let the domain logic manipulate that data before presenting it on the glass. [Fowler02]

Good Points:
Solves the ‘Thin View’ problem. The View has a state, which the Presenter queries, and updates. It is also easier to maintain and understand than is usually the Observer pattern.

Bad Points:
An unorthodox separation of responsibilities on the expense of understandability.

*Model would mean domain model, set aside from the database model. With use of ORM, we rarely see the DAL, but get a set of domain model entities.

02
Nov
07

Application Tiers Muses

MVC?

Recently I’ve come to compare two idioms: MVC and Microsofts’ View-Controller-BLL-DAL. It seems the same, but MVC as coined by ruby is fat-model slim-controller which means, by use of ActiveRecord, we can put more of the business logic that relates to the model INSIDE the model. The controller then serves as a mere coordinator and dispatcher.

With the Microsoft approach, we put only whats essential in the DAL, and have the BLL contain all the ‘active parts’ — the data manipulation and retrieval etc. which are used by the Controller – the codebehind parts of an ASP page, and then rendered into the view – the ASP page itself.

I tend to think the ruby way is more correct.

Scott Bellaware contributed his thoughts on this before and he says correctly that DAL/BLL are dividing responsibility.

Conclusion

I think the fine point here is that having an ORM do the DAL in ruby, changes everything in the argument, since what you type in the ActiveRecord object has nothing to do with the auto-generated DAL code.

So with ruby, the DAL is silently generated below what you are adding to it – to what you think – is the DAL. What you are adding is actually the BLL. Hence we get the layout:

Ruby: View / Controller / DAL(really, its BLL) / DAL(generated)

MS: View / Controller(code-behind) / BLL / DAL

And problem solved, they are conceptually the same.