Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Thursday, February 05, 2009

The Fat Controller must die!

As most parents with little boys know the Fat Controller is a key figure in the Thomas the Tank Engine TV series (and books, toys, clothing, linen, bags, crockery, snacks, etc.). he is a cheery fellow, unfortunately prone to angrily shouting at train engines, but equally kindly and caring about his beloved vehicles (especially steam powered ones like Elizabeth the lorry). For the sake of my children I must point out that I have no problem with this jolly fellow.

fat controller

There is another domain where fat controllers exist, and that is the world of the MVC (Model-View-Controller) pattern. The pattern has experienced a renaissance recently in web applications, particularly because of Ruby on Rails, and now Microsoft's ASP.NET MVC offering.

Now these Ruby on Rails guys have been doing this for a little while longer than the ASP.NET guys, and one key paradigm became clear fairly early on:

"Try to keep your controller actions and views as slim as possible."

This was most clearly explained by Jamis Buck in his excellent Skinny Controller, Fat Model post back in 2006, which is still worth reading even if you don't use Ruby on Rails. More recently Ian Cooper has pointed out this is the same problem webforms had:

"This is the good old problem of domain-logic in code-behind that we had in ASP.NET webforms. Indeed Webforms are just another type of controller, a page controller, and switching to an application controller model does not remove the need for us to watch for domain logic creeping into the controller. There can be a temptation to believe that just because the controller is easier to test it is now safe to put domain logic in there. Do not fall into that trap."

In his book, Domain-Driven Design, Eric Evans identifies this as the Smart UI Anti-Pattern.

“Put all the business logic into the user interface. Chop the application into small functions and implement them as separate user interfaces, embedding the business rules into them. Use a relational database as a shared repository of the data. Use the most automated UI building and visual programming tools available.”

SmartUI
Image from David Hayden's blog

Eric is kind (and pragmatic) enough to point out when it is useful.

“A project needs to deliver simple functionality, dominated by data entry and display, with few business rules. Staff is not composed of advanced object modelers.”

Clearly ASP.NET's webforms model encouraged this sort of practice (it is just far too easy to do), but surely a controller is a safe place for this code? After all, a controller can use many views, so it is more clearly separated than code behind. The problem is that whilst application logic ("Which view do I show next?", "What domain object is handling this request?") makes perfect sense in a controller, there are too many examples of them being used as stores for business/domain logic - which is the province of the domain model.

Hang on, we probably have a (ubiquitous) language problem here. You see, the "Model" in MVC != the "domain model" in Domain-Driven Design (DDD). In fact we need to map the DDD concept of layered architecture to MVC in order to see what we truly have.

MvcMapToDdd

Instead of giving the Model layer a fully fleshed out domain and supporting infrastructure (repository and services), many ASP.NET developers seem to want to treat the Model as pure database infrastructure and DAL (like the one in the Smart UI graphic above). This leads to the problem of where to put the business logic, and the natural assumption is that it belongs in the controller ... which is like making the navigator the captain of the ship.

Ian and Jamis have much more (technical) stuff to say on this subject so I'd advise you to check their posts out if you're into .NET or Rails, respectively, but do take this message with you, the Fat Controller must die!

Thursday, January 17, 2008

New in VB9

I'm trying to do more blogging as part of my personal learning process, so courtesy of my investigation of changes in .NET 3.5 and VS2008, here are some great places to find information for VB.NET:Also, InfoQ has a small editorial piece, Selecting a .NET Web Framework which raises the question of whether having ASP.NET MVC around will make it tempting to give up WebForms and hence our ASP.NET server control libraries? Personally, I think the control vendors will see this as an opportunity to expand their product lines. I think we will see ASP.NET MVC being used more for Web 2.0/externally facing applications and WebForms still being the dominant framework for intranet development, or web-enabled line of business apps.

Tuesday, December 11, 2007

ASP.NET MVC is here

ScottGu has announced the CTP release of ASP.NET 3.5 Extensions. This is great news for ALT.NET and really has made my week. As regular readers might realise the key part of this release for me is the MVC (Model-View-Controller) framework for ASP.NET.
“ASP.NET MVC: This model view controller (MVC) framework for ASP.NET provides a structured model that enables a clear separation of concerns within web applications, and makes it easier to unit test your code and support a TDD workflow. It also helps provide more control over the URLs you publish in your applications, and more control over the HTML that is emitted from them.”
I think this finally gives ASP.NET developers some much needed flexibility. The guys at Microsoft are taking pains to point out this is not a replacement for the existing WebForms model, but they point to the following reasons to use the new framework:
  • It allows developers to use the MVC pattern with ASP.NET more cleanly than ever before.
  • It makes it easier to implement RESTful (Representational State Transfer) systems and to control your URLs using ASP.NET
  • The MVC pattern allows for easier unit testing of business logic behind pages (now in controllers) and therefore helps promote the use of Test-Driven Development with ASP.NET.
I have some personal reasons for looking forward to this framework that may be too politically sensitive for the ASP.NET guys to admit to:
  • This kills off the one form per page restriction, allowing for XHTML that more closely matches the functionality of the page.
  • No more viewdata is a pain, but it also means that ASP.NET developers can now more easily incorporate other client-side frameworks into their applications (e.g. Yahoo! User Interface Library, Script.aculo.us, Prototype, etc)
  • Ruby on Rails is looking a whole lot less attractive as ASP.NET MVC offers much of the same clarity of design whilst leveraging my existing ASP.NET skills.
I will probably devote a few more blog posts to exploring this interesting ASP.NET development, so feel free to let me know (via the comments) what you might be most interested in knowing about.

Thursday, July 05, 2007

New .NET Role

I'm about to start a new permanent role at a .NET web company in Sydney. I've been lamenting the fact that I won't get to play with Ruby on Rails at work, mainly because I love the simplicity of the Model-View-Controller architecture it forces on you. Then, lo and behold, what should I stumble across but MonoRail, a project to bring the MVC web framework of ActionPack (what drives Rails) to .NET (1.1, 2.0 and Mono).

So I may end up escaping WebForms horror of one-form to a page, abstracted abstractions, and the cross-browser nightmares that follow.

Well, OK, a guy can dream can't he? ;D

[UPDATE: That sounds like I'm not excited about the new role, the fact is I am, especially because we'll be using Scrum, and the guys seem friendly and smart. It's just that I was coding dynamic web pages before WebForms came along, and I hope to be doing so well after they're relegated to the scrapheap.]