Just sharing some of my inconsequential lunch conversations with you... RSS  

Saturday, October 04, 2008

A common MVC mistake

One of the first errors I've detected on my early MVC code was the tendency to load some list of values directly on the view. For instance:

    <%= Html.FormHelper().Select(
"MyListOfValuesID",
Web.Factories.ServiceFactory.Resolve<IMyListOfValuesService>().FindAll(),
"Name",
"ID",
new Hash(selectedValue => new[] { ViewData.Model.Item.MyListOfValuesID }, firstOption => "")
)
%>


This loading is clearly a controller's responsibility, but I've found it very easy and natural code to write. Too easy.



That brings's me a question: aren't we making a design error when we write controller and views on the same assembly? Shouldn't we make sure all behavior a view could get their hands on should be given by controllers, and never by services? We do it for ages on other layers, for instance we don't expose data layers to UI.



That said, it's really strange no MVC project I got my hands on has ensured this isolation. Probably because these project are all demos, and demos have to be short and concise.

No comments:

Development Catharsis :: Copyright 2006 Mário Romano