Mvc ninjatips

by Christer 28. November 2009 22:41

I was lucky enough to get to check out Scott Hanselman's Pdc session "ASP.NET MVC 2: Ninjas Still on Fire Black Belt Tips" and got really impressed with a few of the features he mentioned.

First thing that hit me was DataAnnotations. I had seen it earlier but i wasnt that impressed as it looked like an easy way to do general validation on serverside, but i couldnt see any good use in the real world. But i changed my mind asap after this session =). If you havent seen DataAnnotations at work before i suggest you check out Johan Driessens excellent post about it at: Testing DataAnnotation-based validation in ASP.NET MVC

So, the "flaws" i noticed the first time i read about DataAnnotations where that it works nicely for code i write myself. But how about generated code? i dont want to modify generated code as my modifications will be removed when i regenerate it. Or if i dont have the code? it cant be used at all then. And lastly, if i dont want to mess up my nice clean code with a bunch of attributes used for validation..
The solution: Buddy classes. By using the attribute MetaDataTypeyou can specify another class that have all the metadata for your object. Since its in another class now it doesnt pollute your code anymore, and it maps them by name, so you can map them to stuff you know will exist in the future.
Also the annotations are extendable so you can write your own validators and they can be generated to clientscript if you want to use that (or write your own).

Another cool feature mentioned in that session was the DisplayForModel/EditorForModel-helpers that use templates to render your stuff.
An empty call to Html.DisplayForModel()/Html.EditorForModel() will use the modelname to look for a template. So it will try and load an ascx-file named <modelname>.ascx and use it to render \o/. You can also specify a name to load a named template.
And for properties there is an attribute you can use (UIHint) to specify a template for them aswell.

The coolest stuff of all is that since it tries to load a template for types you can use it on all existing types aswell! So by creating for ex a DateTime.ascx template you can specify how all rendering of a DateTime will be done.

As a sidenote it also supports fallback. (not mentioned in that session) so if you have an Employee that inherits Person, it will use Person.ascx if it cant find the Employee.ascx template. Or if you have a string it will fallback to object.ascx if it cant find your string.ascx! \o/

So all in all a nice bunch of simple tips that help keep your code clean and help you avoid repeating yourself, and im a big fan of both =).

Other stuff he mentioned was Customizing WebFormViewEngine to select templates depending on custom parameters. Using MvcTurbine to hook up IoC containers. That you can use <%: Model.Prop %> instead of <%= Html.Encode(Model.Prop) %> for automatic HtmlEncoding \o/. And some cool info on customizing the templates used to generate the default code (T4 templates).

If any of the above sounds interesting i suggest you take a look et the complete session available here http://microsoftpdc.com/Sessions/FT59Its about an hour and worth every minute spent watching it.

Tags: , , ,

Development

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 Sweden License.


Welcome to the Dropit blog!

Here we, the people that work at Dropit, will write about stuff that interests us. For example web development, especially with .NET and EPiServer - but we'll also talk about other techniques that interest us, marketing on the web, social phenomenons, pop culture, games and software development in general.