1

I have a c# webapi application which has magic strings and numbers everywhere. I am asked to change this by having a string manifest or a bundle which holds these strings in one place and refer them. I don’t want to use Enum. I don’t know what is string manifest or bundle.

Can someone please give me an idea on how to do this with an example. Basically I need to avoid the hard coding throughout the application and use an external configuration for these strings so that whenever these string values change, we don't have to compile and deploy the .NET application.

Idea is to have all the expected responses in some kind of bundle or string manifest and refer to that rather than have magic numbers and strings everywhere.

I need to remove the magic case numbers and FreeText and instead configure those values in a single place and refer them without having to deploy the application again.

switch (ReasonCode) 
{ 
    case 0: 
        FreeText = "Passed. Please proceed"; 
        break;
    case 1: 
        FreeText = "Fail"; 
        break;
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
Sosa
  • 21
  • 2
  • 1
    Magic strings (or numbers) in code is generally bad, but to replace them with a string manifest or bundle sounds wrong too. Perhaps you misunderstood the request? – Neil Feb 05 '20 at 16:15
  • Can you [edit] your question for describe better what your application does and how you're handling this magic strings? - it is difficult suggest any of you don't provide useful information. – Marco Aurelio Fernandez Reyes Feb 05 '20 at 16:16
  • 1
    Why cannot you use `resource` file to define your strings? – MKR Feb 05 '20 at 16:16
  • Resource files in combination with caching would be one way, however, can you offer more insight to what you are attempting to accomplish? – Ross Bush Feb 05 '20 at 16:19
  • @RossBush Resource files are already cached (in memory), so there is no need to add an extra layer. – Neil Feb 05 '20 at 16:20
  • Can you give some examples of the magic strings; context will matter. – ChiefTwoPencils Feb 05 '20 at 16:21
  • @Neil - You are correct. I was very general with the term caching. I meant more on the lines of caching one blob of similar resource strings in json format or whatnot. – Ross Bush Feb 05 '20 at 16:24
  • @Neil Idea is to have all the expected responses in some kind of bundle or string manifest and refer to that rather than have magic numbers and strings everywhere.(eg:switch (response.ReasonCode) { case 0: response.FreeText = "Passed"; break;} – Sosa Feb 05 '20 at 16:39
  • Sounds like you need .net resource files ( https://learn.microsoft.com/en-us/dotnet/framework/resources/ ) which means you can do things like `ResponseText = Strings.GetString(Passed_Please_Proceed);` – Neil Feb 05 '20 at 16:46
  • @ChiefTwoPencils I have edited the question with example . Please check. – Sosa Feb 05 '20 at 17:27

1 Answers1

0

Enum are the preferred way to store magic constant when they are integers.

For string constant, the simple way to do it is one or more static class that expose the code:

public static class ApiCodes
{
  public int RootUserCode { get; } = 42;
  public string DeleteActionCode { get; } = "REM";
  // ..
}

A magic code value change can be done in a single place, and the code is pretty readable:

public void DoAction(string actionCode)
{
  if (actionCode == ApiCodes.DeleteActionCode)
  {
    ..
  }
}

Edit:

With singletons and a static class at root level, you can also have a hierarchy:

public static class ApiCodes
{
    public static ActionCodes ActionCodes { get; } = ActionCodes.Instance;
}

public class ActionCodes
{
    public static ActionCodes Instance { get; } = new ActionCodes();

    // Singleton: private constructor
    private ActionCodes()
    {
    }

    public string Add { get; } = "ADD";
    public string Delete { get; } = "REM";
}

And use it like that:

public void DoAction(string actionCode)
{
  if (actionCode == ApiCodes.ActionCodes.Delete)
  {
    ..
  }
}
Orace
  • 7,822
  • 30
  • 45