M-V-VM in Asp.Net MVC : removing dependencies between asp.Net views and controller actions

by Tom 30. November 2009 06:32

Since the early days I have always been a fan of the ASP.net MVC framework, although I had one really big issue with it : the coupling of the controller actions in the view pages. This part was a real annoyance, since all the flow and logic was included in the controller, and none of it in the views, _except_ the action flow; this was included in the views by using Html.ActionLink.

This has always been a nuisance for me, so last week, as I was working on a project, I decided to fix this issue...

IMHO the possible proceeding controller actions in the program flow should be defined in the controller action, and not in the view. So how could we do this, while still making it maintainable and easy to follow ?

After considering a few alternative routes, and thinking about it event more, it suddenly hit me; why not represent the possible routes as data.

A few development cycles later I had the following code in the controller:


public ActionResult Index()
{
     ViewData.Model = new VMIndex()
     {
         AllTasks = rTask.Find.OrderBy(o => o.Name),
         AL_AddTask = this.ActionLink("Add new task",c=>c.AddNewTask(null,null))
     };
     return View();
}

And this code in the view :


<h1>Task list</h1>
<%= Html.Grid(Model.AllTasks)
        .Columns(c=> {
            c.For(t => t.Name);
            c.For(t => t.Description);
            c.For(t => Html.ActionLink(t.AL_Status())).Named("Status").DoNotEncode();
            c.For(t => Html.ActionLink(t.AL_Edit())).Named("Edit").DoNotEncode();
            c.For(t => Html.ActionLink(t.AL_Delete())).Named("Delete").DoNotEncode();
        }).Empty("No tasks yet")
%>
<hr />
<h3>Add a new task</h3>
<% using (Html.BeginForm(Model.AL_AddTask)) { %>
    Name<br />
    <%=Html.TextBox("Name") %><br />
    Description<br />
    <%=Html.TextArea("Description") %><br />
    <%=Html.SubmitButton(Model.AL_AddTask) %><br />
<% }%>

Which looks rather nice in my opinion. Since I do not know whether I am the first person who thinks about this or not, I decided to publish the sourcecode together with an example on github.

The technical implementation is actually quite simple; I implement the Model-View-Viewmodel pattern, which is used in WPF, using a simple wrapper class for the action links (i.e. commands in MVVM). Once I made that link, the rest was childs play.

Next to this I also created some helper extensions, to make the code a little easier on the eyes.

I also think that by using this pattern we should be able to implement a winforms/WPF app using the same controller. Anybody who is willing to send me a git patch of a WPF application using this controller: please do !!!

PS: This also makes the actionlinks testable in an easy way; another responsibility that has been taken away from the view !!!

Bookmark and Share

Comments

2/5/2010 5:31:42 AM #

trackback

Build asp.net MVC applications FAST with  MVCExtensions v0.4

Build asp.net MVC applications FAST with  MVCExtensions v0.4

Tom's ramblings |

About Tom

Tom Janssens op LinkedIn

Tom Janssens op twitter

Core bvba RSS

 

Tom Janssens is an independent freelance ICT consultant that has been "into computers" ever since the age of 7.

Typing source code from a book evolved into exploring the limits of coding in procedural, assembly and object-oriented languages.
As he matured in software coding, he started focussing on the problems surrounding software development, and learned that software development is usually about people and interactions first, and about technology second.

Due to his diverse track record he gained insights in a lot of aspects of the software development process. Currently his main focus is on strategic ICT advice, lean product/project development and improving the software development process and architecture.

He avoids ivory-tower-approaches by applying and verifying the applicability of the latest tech buzz in software experiments.

He is also the founder of the following LinkedIn groups:

CQRS Professional
BDD Professional
Asp.Net MVC professional

More info about Tom and his company...


Advertisement

Forget all your SCRUM -, Kanban - and other Agile and Lean certificates

Here is the only true AGILE and LEAN certificate you will ever need:

The Creative Recursive Analysis Process Certificate
(CRAP Certificate for short)

More info can be found at the official CRAP certificate website:
http://bit.ly/CRAPCertificate