Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good idea to use string constant keys to avoid magic string keys?

Tags:

asp.net-mvc

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?

like image 394
LaTeX Avatar asked Feb 14 '11 06:02

LaTeX


People also ask

Are string primary keys bad?

Yes, from a performance standpoint (i.e. inserting or querying or updating) using Strings for primary keys are slower than integers.


1 Answers

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.

like image 78
Jakub Konecki Avatar answered Nov 15 '22 11:11

Jakub Konecki