Saturday, April 23, 2011

MVC cardinal rule, View doesn't have any logic

The View of MVC...

public ViewResult List()
{
    using (var s = SessionFactoryBuilder.GetSessionFactory().OpenSession())
    {
        var products = s.Query<Product>().Fetch(x=>x.Category).ToList()
        if (Browser.IsDesktop)
            return View(products);
        else if (Browser.IsMobileSafari)
            return View("ListUsingMobileSafari", 
                new ProductMobileViewModel 
                {
                    Products = products,
                    IsRetina = Browser.IsRetina
                });
    }
}

...renders any Model to its proper presentation, View doesn't need to handle(and must not) any logic, so as to keep proper separation of concerns. The UI(View) team for mobile Safari, could merrily do their approach without bugging the logic(Controller) team for other informations, as the business of gathering those informations are promptly prepared by the controller team, which in turn are driven by business requirements and user specifications.

Never in an ideal development shop that the View should deal with disparate models and other informations by themselves. The Model(and ViewModel) is the contract between the Controller and View.

No comments:

Post a Comment