I have seen this many time in many places. And heard many different arguments to validate the reasons for it too.
I am talking about having hard coded values or string in your code.
It’s not like the people who were coding it don’t know that it’s a bad idea, but sometimes some developers lacks the time and motivation to implement a separate way to handle this issue. Matter of fact sometimes they are just not empowered enough to implement a better solution.
Depending on how complicated your project is, you can always create a static class in your project to read from a config file or you can also create a simple Class library where you can have logically organized enums and then use you as you need.
I will give you few example here
#1 When you are going with the app.config and one static class in your project.
You might have your AppSettings.config like this…keeping it simple
1: <appSettings>
2: <add key="LoginErrorMessage" value="Invalid Login. Try again."/>
3: <add key="Location" value="United States"/>
4: </appSettings>
And your static class will look like this
1: public static class GlobalSettings
2: {
3: private const string loginError = "LoginErrorMessage";
4: private const string location = "Location";
5:
6:
7:
8: public static string LoginError
9: {
10: get
11: {
12: string value = ConfigurationManager.AppSettings.Get(loginError).ToString();
13: return value;
14: }
15: }
16:
17: public static string Location
18: {
19: get
20: {
21: string value = ConfigurationManager.AppSettings.Get(location).ToString();
22: return value;
23: }
24: }
25: }
#2 When you want to use a Class Library
You might want to create a separate Enum class library to keep track of your values.
- Create a Procject name (example) MyEnums
- Create static class Enum
Depending on how many different segment you have you might want to seperate them logically.
For example I have one called Indicators.
Here is an example class. This is just for this blog example.
1: public static class Indicators
2: {
3: public enum StatusType
4: {
5: Active = 1, Inactive = 0
6: }
7:
8: public enum Transactions
9: {
10: Inprocess =1, Processed=2,Denied=3,Archieved=4
11: }
12:
13: public enum Login
14: {
15: LoggedIn=1,LoggedOut=2,LockedOut=3,PasswordLocked=4,PasswordResetRequired=5, PasswordResetDone=6
16: }
17: }
Now you can reference the project to your client project and use the value.
#3 Hybrid
You can always use a hybrid model where you get the values on your class library from different appsettings and enums as needed.
You should always cater this to your specific need.