Learning Asp.Net MVC Framework
This is basically a “What is Chris up to now?” post. As the title suggests I’m going up to my ears into Asp.Net MVC. I’ve been doing Asp.Net WebForms for about the past 8 years, so this is a good time for a switch. Lucky for me, I’m armed with a good book: Pro Asp.Net MVC Framework by Steven Sanderson. I’ve read about have of it and I love it. The greatest thing about the book is that it actually answers questions as you think of them. A remarkable achievement for any technical book.
But here is where I’m at right now (btw: if I get anything here completely wrong, blame me, not the book author):
1. Routing. I’ve already played with routing before with standard Asp.Net WebForms, so the concept is not new to me. But getting to the point where I really grok Routing will be a while. The basic concept is this: Routing is one of the first control points that you get with Asp.Net MVC (or WebForms now as well). When a request comes in (web browser requests a page) the Routing engine determines which controller (or web form) will handle the request. With this in place you can now have “pretty” urls. So something like http://www.elegantcode.com/Chris/is/cool. That link wont work, but that is the general idea. More often than not you see something like this: http://localhost/Customer/Edit/ChrisBrandsma
* my current issues: none really – so long as I can manage to follow the existing route paradigm. Passing extra parameters can be strange, but the good old ?id=5&sid=6&… still works. Also, as soon as you head into Routing land you suddenly have to start thinking about your urls. In traditional WebForms world, a url is like a latitude/longitude – and the user has to figure out how to get there. In Routing land, the url is a list of turn by turn directions.
2. Controllers. Controller are the general housing points for web application flow. If you look at the second url, you see /Customer/Edit/ChrisBrandsma there. The Customer part of the url is the controller. Why? Because my Routing setup says it is. That’s why. Controllers hold Actions. Edit is the action. ChrisBrandsma is just some data I’m passing to the action.
* My Current Issues: I have not reached a Zen of when to create a new controller yet. I keep thinking of single responsibility, so right now my controllers tend to have few methods in them. But I’m still getting the hang of it.
3. Actions. Actions are things the Controller can do. Back to the url example: http://localhost/Customer/Edit/ChrisBandsma, there is an expectation I will be editing myself (I would like 50 more IQ points and Kung Foo). Edit is the action here. There is also a convention that states there should be an Edit.aspx page to go along with the Edit action. I can override that, but that is the expectation again.
* My Current Issues: I’m barely scratching the surface with this one. I am trying very hard to keep my Action code small. To that end I’m creating ControllerActionPresenters and ControllerActionViewModel for each Action I create. This might be overkill, but it is keeping thinks clean right now.
So now my Actions have three tasks: get user data (querystring, form, session, etc), get the presenter and pass it the user data, return the ViewModel data to a specific view. I’m ok with this so far.
Also, I’m doing everything I can to use typed Views. This would be insane without typed views.
* My (other) current Issues: so far I have not tried to do a data entry form with MVC. Until I do that, more of this will be a mystery to me.
My thoughts so far. I still like it. There are some learning curves ahead for me yet, but I enjoy that. Either I will savor the sweetness of WebForms from the experience, or look back at them and laugh. I’m starting to laugh.