This is not a problem, but you're right that it can be tricky to maintain.
Some people like to do something more "object oriented". So I would have a method for each case, but I think it's an exaggeration.
You have to analyze the specific case and I do not have the information at all. I do not know who can add something. Can you let the user do this? Even if you are a more privileged user. Or do you need to pass the programmer's approval before putting this in?
Is there no exception if the action to be taken is different from the one in the example?
If it is really common to add new items and this is something that the user can do, the correct thing is to make a registration of equivalence codes. It would have a table in the database with the code found and the equivalent to be replaced. There it makes a screen for someone to update that. Maybe just for someone privileged. Below I show you how to do the action shown in the question.
If the programmer has to be aware of this new code, I see no problem doing this in the code.
If you want to avoid both if
you can make a list or dictionary with the codes and make a loop. It would look something like this:
foreach (var item in codigos) {
if (_strLinesFinal.Contains(item.Key)) {
_OS = _OS.Replace(item.Key, item.Value);
}
}
To initialize the dictionary would look something like this:
var codigos = new Dictionary {
["M50"] = "T90",
["M60"] = "T91",
["S555"] = "C100", };
If it is taken from a database instead of these equivalent codes it is in the application code, it will be created by reading the database. You can even use the database directly and create the dictionary. It would have to change something, but it works. Depends on the case. I would probably do the cache in the dictionary. It depends on the database technology you're using, but it would look something like this:
var codigos = new Dictionary<string, string>();
using (var cmd = new SqlCommand("select codigo, equivalencia from codigos", dbConn)) {
using (var reader = cmd.ExecuteReader()) {
while (reader.Read()) {
codigos[(string)reader["codigo"]] = (string)reader["equivalencia"];
}
}
}
But if you have actions other than just giving Replace()
then if
will be necessary.