My specific question is about ASP.NET MVC, but I'm sure the answers can be applied outside of ASP.NET.
How do you feel about functions that take string arguments, but still want to keep it safe?
In ASP.NET Html.ActionLink extension method takes two arguments of type string: string linkText, string actionName. The first argument is the text that can be loaded from the resource, but the second argument is the name of the method that exists in the code.
One option is to create a class with constant lines, so instead of writing
Html.ActionLink("Blah blah", "MyAction");
can write:
Html.ActionLine("Blah blah", StrongType.MyAction);
Where MyActionis the string const in the class with the name StrongType. However, this becomes a pain for control when the number of methods and other objects that can be passed as arguments increases.
The second option is to load the function name somehow using Reflection, but this seems too expensive.
Is there a better way to do this? How do you make sure you don't have typos in string arguments like the ones mentioned earlier?
source
share