chkal / mvc-spec-migration-test

0 stars 0 forks source link

Discuss URI to action mapping #2

Closed chkal closed 9 years ago

chkal commented 9 years ago

Original issue MVC_SPEC-2 created by Santiago Pericas-Geertsen:

One of the core aspects in an action-based MVC framework is URI mapping. Every request received is mapped to an action, which in our case takes the form of a Java method. This mapping can be defined in multiple different ways:

(a) Using an external config file, such as an XML file (b) Via annotations on classes and methods (c) Programmatically using an API

An example of (a) is what earlier versions of Spring MVC did; an example of (b) is what is done in JAX-RS, Spring 2.5 and later and VRaptor 4, and an example of (c) is what is done in frameworks like Express.js in Node.

Option (a) is the less modern approach and both Spring and EE have been migrating to (b) to reduce the need for config files. Option (c) is interesting but more applicable to other languages like JavaScript. Having said that, in JDK 8, with the addition of lambdas and method references, it is also possible:

mvc.map("/foo/{id}", MyController::getId);

Despite that, option (b) is probably the best fit for Java given that it does not require additional classes to be defined (assuming class scanning) beyond the controller class itself.

There are two aspects when it comes to URI mapping, syntax and semantics. Syntax refers to the actual annotations used to define the mapping, while semantics refers to the algorithm that interprets those annotations and carries out the mapping itself. Both are in scope for this task.

(1) Should MVC use approach (b) describe above?

(2) Should MVC define its own set of annotations for this mapping or re-use existing ones like those in JAX-RS?

(3) Related to (2). What about the semantics of the mapping algorithm itself? The JAX-RS mapping is fairly complete in that it supports content negotiation as well as resource locators (See 4).

(4) Should there be support for dynamic mappings? JAX-RS enables this using resource locators, which can execute code during the mapping phase and, thus, alter the outcome of the mapping.

chkal commented 9 years ago

Comment by ivolimmen:

My 2 cents: 1) I would support both option b and c. Option B would definitely be the modern approach but a programmatically option would also be a very welcome option. Certainly a very useful addition if someone would create a framework based on the MVC specs. 2) I opt for as much reuse as possible. But it has to be logical. The reuse of the annotations of JAXB for JSON for instance is a bit weird. 3) No strong opinion on this one. 4) Dynamic mapping as explained sound very much like routing in an mvc application. So yes I am for this. In the old Struts there was a ActionDispatcher that mapped a part of the URL to a method in the controller. Much like ASP.NET MVC does as well; every controller is directly mapped to the first part of the URL and every subsequent path is mapped to a method. It depends on what kind of MVC you wish to design and what the targeted users are.

chkal commented 9 years ago

Comment by Santiago Pericas-Geertsen:

Provided by JAX-RS.