Nifty snipplet to fill data into a dynamic type

So I'm trying to put together a fairly major update to BarelyMVC so that I no longer have to resolve strings to their actual types.

I realized that this is an extremely common thing that must be done. Any ORM worth it's weight in salt does this, as well as many other things. So, I'll go ahead and give everyone who hasn't done this a nice starting point. As you can tel, my code is geared towards my router, but easily adjusted. The only big thing missing is a way to specify a custom converter. I'll probably update this code snipplet when I get that figured out.

I decided against containing the code here as I have no syntax highlighting, so check it out as a gist

Anyway, what this will eventually allow is instead of making code like this:

//in an HttpHandler
int tmp=0;
if(!int.TryParse(RouteParams["id"], out tmp))
  throw new HttpException(404);
//tmp now contains a valid integer

instead, you can just create a route parameter model like so:

class BlogRouteModel
  public int ID {get; set; }

and then be able to do something like

Lookup(RouteParams.ID); //contains a valid ID

I haven't figured out the exact way this should all tie together. Apparently generics everywhere isn't the solution as I quit after I would have to somehow contain a list of generic lists with the lists being of a generic type and so on.. It was so fugly.

Also, this same type of convention will be used for HTTP Form variables, so no more parsing of integers and making sure things exist and such there.

Posted: 10/14/2012 12:55:57 AM