My idea is to avoid magic string keys in my Asp.Net MVC application. To do so, I want to create string constant keys to be shared in the application.
For example, I can write TempData[MyConst.Message]
instead of TempData["Message"]
.
public class MyConst
{
public const string Message = "Message";
}
My questions are: Is it a good idea to do this way?
Yes, from a performance standpoint (i.e. inserting or querying or updating) using Strings for primary keys are slower than integers.
Instead of custom classes put your strings in a resx file. VS will generate a class for you and it will be easier for you to translate them into a different language in the future should the need arise.
There is nothing specific you need to do for MVC. Take a look here:
http://msdn.microsoft.com/en-us/library/fw69ke6f(v=vs.80).aspx
http://support.microsoft.com/kb/917414
http://msdn.microsoft.com/en-us/library/ms227427.aspx
For 'internal' usage I would prefer Enums over strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With