Specify ASP.NET MVC 3 Routes symbolically -
Specify ASP.NET MVC 3 Routes symbolically -
i asp.net mvc 3 framework. or @ least, sure incomparably improve trying fool asp.net 3.5 or 4.0. however, confused something. why did take specify routes strings?
iow, told specify routes (for instance):
... new { controller = "products", action = "list", id = urlparameter.optional }
this route matches productscontroller.list() method. let's i'm in refactoring mood , want rename productscontroller inventorycontroller. after using renaming tool of choice, have open global.aspx , go through routes , alter these stupid strings "inventory". might respond can find , replace... come on! sense such last-generation answer.
i love refactoring code come understand domain better. don't want utilize stupid (i stupid because have no significance compiler) strings refer symbolic/semantic code constructs correspond type , method names stored in symbol table. point? why bother types @ all? let's go writing scripts using associative arrays , dictionaries represent our domain model... seems me benefit of strong typing lessened when mix string references.
seriously, though, alternative reflection. there performance nail this? suppose mvc framework must using reflection on "products" string productscontroller, so... also, have remove "controller" portion of type name, follows:
= typeof(productscontroller).name.replace("controller", string.empty)
i utilize next helper function create little dryer:
public string getcontrollername(type controller) { homecoming controller.name.replace("controller", string.empty); }
benchmarking in order, if way avoid strings... still, stupid. i'm using reflection on type string mvc going utilize in conjunction reflection type had in first place.
is there reason not take next (logical?) step , have controller , action properties expect types , delegates directly? wouldn't cleaner , clearer? far understand, fundamental aspect of mvc convention on configuration, routing strings seems furtive form of configuration me.
is there way around this? still new mvc. i've read can replace these routing components. know if possible i'm talking about? , if it's not possible, well... lone here? missing something? overriding reason why essential these routes set dumb strings? if not, maybe lobby for?
am lone in hating when strings used in way? still think c# needs akin ruby's symbols , lisp's keywords our refactoring tools take advantage of. sort of "string enumerations" enumeration value name @ same time value.
i understand parts of question subjective, looking objective reply on whether possible straight utilize types , delegates specify these routing configurations.
thank you, jeromeyers
personally never had problems way routes defined because unit test them. if in refactoring mood, unit tests guarantee me routes behave want.
of course of study if still not satisfied way asp.net mvc team designed framework (don't hesitate open ticket , vote improvement in future versions) write custom route:
public class ihatemagicstringsroute<t> : route t : controller { public ihatemagicstringsroute( string url, expression<func<t, actionresult>> look ) : base(url, parse(expression), new mvcroutehandler()) { } private static routevaluedictionary parse(expression<func<t, actionresult>> expression) { var mce = expression.body methodcallexpression; if (mce != null) { homecoming new routevaluedictionary(new { controller = typeof(t).name.replace("controller", ""), action = mce.method.name }); } homecoming null; } }
and instead of:
routes.maproute( "default", "{controller}/{action}", new { controller = "home", action = "index" } );
you could:
routes.add( new ihatemagicstringsroute<homecontroller>( "{controller}/{action}", x => x.index() ) );
now can rename controllers , actions much like.
asp.net-mvc-3 asp.net-mvc-routing
Comments
Post a Comment