Question

I am exploring different MV* patterns and noticed that MVP is used very heavily in .Net applications but almost no where else.
The only other big framework that seems to incorporate it is GWT but only in some parts.

  1. Why is MVP such a microsoft thing (why is it not nearly as popular in other languages)?
  2. Is there a nodejs app/project that uses MVP (preferably with source i can look at)?
Était-ce utile?

La solution 2

There weren't any great examples of MVP in nodejs. We did a bunch of research and implemented the TODO app using the MVP pattern in nodejs and .net.

Its not perfect yet, but I think it fills a gap. Pull requests are welcome.

https://github.com/EchoGlobalLogistics/mvp

Autres conseils

To speak about the reason of it looking like a Microsoft thing, can be rooted to the design decisions of at least 2 large companies and their respective frameworks. Microsoft incorporated MVP into .NET. While Apple opted for MVC in Cocoa.

As for JavaScript, take a look at the recent framework Riot.js https://moot.it/blog/technology/riotjs-the-1kb-mvp-framework.html

Although it's not particularly a "node.js framework", it is an example of something moving towards this term of "Isomorphic JavaScript" (JavaScript layer ran on the client + server), and what can be brought to the table by attracting many engineers from many different experiences and backgrounds.

From that article:

Riot models define your application. It's your business logic exposed to outer world with a well-thought API. A completely isolated, testable unit that can be run in browser and server (node.js).

From my experience, and please pardon my forthcoming general blanket statement, the reason why MVP isn't seen that much in this technology is because many people either haven't heard about it, forgotten it, or simply don't see much difference from what a "Controller" is and what a "Presenter" is. This doesn't mean that there isn't a difference, and Tero Piirainen has outlined things fairly well in the Riot.js article for people coming from other JavaScript MVC frameworks.

It's definitely possible to use Node in an MV* fashion. Here are a few really good questions/answers that I've used as a guide when structuring my applications:

How to structure a express.js application?

ExpressJS How to structure an application?

There are a few frameworks for Node as well that are MV* based:

  • Matador
  • The new Sails.js (Based off of Rails)
  • Most node frameworks are built on Express.js, which can be adapted to be MV* as explained in the answers above.

Here is a sample app built on pure express in an MV* fashion.

tl;dr Look at flatiron framework especially at CLI plugin. Also this article https://blog.nodejitsu.com/writing-cli-apps-with-flatiron and its example section.

Answering your first question I'd say that MVP is not all that microsoft, even the wikipedia article states that. It's just language designers in Microsoft have chosen the paradigm of hiding details of widgets implementation.

When you as a user interact with a widget there is a lot of stuff going on. And to some extent it may seem that widget itself can be seen as a little application with it's own business logic model, with it's own view and controller. Think of for example a dropdown: it has a set of methods to actually draw some rectangles on the screen, it also has methods to present a list of values as text, and when you click on some item there is changing color of background and text going on which happens according to certain rules (think business logic). Microsoft's .Net platform has a lot to do with user interface. This is why it's so important to choose a paradigm that will fit that purpose better.

Node on the other hand is a completely backend platform, it's not a framework for creating web applications. I'm not saying that you can not or should not create a web app with it. What I'm saying is that a role of Node code in web application should be limited to IO. Getting requests, sending responses, sending/receiving data to/from storages, maybe parsing. And you can create a framework for creating web applications on top of node of course.

Trying to follow MVP while creating a web application you will probably want to have Presenter as close as possible to the actual rendering of the widgets. In case of web applications that would be a browser. Node in this case would play a role of REST server (or any other preferred way of data exchange can be used instead of REST here).

There is a different class of applications that Node can be used to create. Those would be CLI apps. This is where you can create applications using MVP and having the Presenter in Node. To look for examples I'd suggest to look at flatiron web framework which has a good support for creating cli apps and also has few examples of such apps: https://blog.nodejitsu.com/writing-cli-apps-with-flatiron.

Also there is https://github.com/mscdex/node-ncurses. This can give you a chance to "roll your own MVP". Which is good practice when your goal is to explore the thing.

We recently used nodejs with express for building a web app. While working on it, we built a small framework on top of express to organize our code better. The framework forces you to write separate handlers (Presenters) for GET requests and Commands for POST requests. (See martinfowler.com/bliki/CQRS.html). The framework also forces Post-Redirect-Get pattern (en.wikipedia.org/wiki/Post/Redirect/Get) by only allowing redirects from Commands. The npm package is expressmvp (https://www.npmjs.org/package/expressmvp).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top