Friday, May 27, 2011

ASP .NET MVC 3

I've recently been going thru the FREE .NET training from pluralSight available at http://www.asp.net/mvc.

The training shows you how to download free versions of Visual Web Developer, IIS and SQL Server for development purposes.  There are 10 classes, each about 45 minutes long and they take you thru basic tool setup, simple web application development, the MVC framework, Unit Testing with NUnit and .NET Entity Framework (Code First model).

I am struck by the similarity between .NET MVC 3 and Spring framework 3.

Request Routing
Both frameworks route requests based on the URL.  For example a url with /Home/Search will route to the HomeController (*Controller added by convention) and the Search method within the controller.  Methods can be annotated to determine different variations for http get versus post.

Parameter Marshalling
Parameters required by the method (e.g. Search( String term)) will be gleaned from the post body or the url parameters and automatically marshalled into the method.   Even complex types like Customer will be automatically marshalled.  This is nearly identical between .NET and Spring.

Model Validation
Spring uses the @Valid annotation on method parameters to automatically invoke model validation or it can be programmatically initiated.  .NET allows programmatic initiation.  Model errors are added to the ModelState and the form redisplayed via return View();  The delivered javascript libraries from Spring and .NET (both JQuery based) display the errors on the correct place on the form.  Spring also has jsp tags to process the model errors which is better I think, as it is less javascript on the client.  Similarly, .NET allows client side validation to be turned off and let the Razor engine put model validation errors where they belong.

Model Composition
Once inside the controller, persistence processing (add/update/delete/select) can be done and a new Model can be built for use by the next view.  The model and view are combined and returned as a result of the controller method.  For example -- return View(model); or return JSONView(model);  The page template is selected from the origianl URL, for example /Home/Search will route to the HomeController.Search method and return the /home/search.cshtml page with the model injected.  The cshtml uses the Razor engine to take the model and put it's attributes on the form, much like Spring's JSP binding.

Entity Framework
Spring will use JPA/Hibernate whereas .NET can use the EntityFramework.  Both have annotated mappings of entity attributes and relationships that are very similar.  Both have the notion of an entity manager that will take care of persisting data changes without the programmer needing to be worried about connection pools and the like.  Both allow validation annotations such as required, regex matching, min/max, etc.  Both allow extension to provide your own validation rules.  Searching is performed via LINQ in .NET and JPA Criteria API in Spring.  Both use a similar idea of creating a Query using a QueryBuilder that you add .From, .Where, .And, .OrderBy clauses and the like.  The entity framework takes care of translating the query to the particular database dialect you are using and issues the query, gets the result/s and marshalls them into the appropriate entity classes.

Overall, I have really been amazed by how similar the two frameworks are.  I've long been a JSF developer and feel that JSF kicks JSPs butt.  However, if I want to be skilled in a way that allows me to develop for .NET and Java, the Spring and .NET MVC frameworks really position me to do just that with a marginal amount of re-training.

2 comments:

  1. Hello Scott,

    I want to ask you about the difference, I have been a .net developer for past years.

    Currently,I am interested on ASP.NET MVC3 and I have also studied and developed small sites through it. But, side by side I want to migrate also into the java world.

    So considering the similarity with the ASP.NET MVC, which one do have more similar paradigm to ASP.NET MVC, Spring or JEE6.

    When I will develop through the java technology I would like to be at home with my past experience in .NET.
    In your article you also described the Spring framework seams similar to the .NET.

    So is it Spring preferable to JSF considering the learning curve for .NET developer?

    Thanks

    ReplyDelete
  2. Spring MVC is a clearly delinieated MVC 2 pattern, i.e. the is a Controller, Domain (JPA Objects) and Views (JSP). It is very much request/response based. JSF MVC is thru binding form controls to backing beans with navigation controlled via the backing bean or externalized xml file. JSF performs partial page updates easier due to the statefulness of the UI component tree on the server. It feels a little more the MVC 1 pattern from the SmallTalk days.

    You are probably more familiar with MVC2, so go with Spring. If you find Spring difficult to configure and get started with take a look at Spring ROO or Grails. Also SpringSource Tool Suite has good Eclipse IDE configuration for Spring, ROO and Grails. IntelliJ is nice too.

    ReplyDelete