That’s not a problem, but you’re right that it can be difficult to maintain.
Some people like to do something more "object-oriented". So it would have a method for each case, but I think an exaggeration.
You have to analyze the specific case and I don’t have the information of the whole. I don’t know who can add something. Can you let the user do that? Even if you are a more privileged user. Or you need to pass the programmer’s approval before you put this?
There will be no case that is exception and the action to be taken is different from the one in the example?
If it is really quite common to add new items and this is something that the user can do, the correct thing is to register equivalence codes. There would be a table in the database with the code found and the equivalent to be replaced. Then make a screen for someone to update this, maybe only for someone privileged. Below I show the basis of how to do the action shown in the question.
If the programmer has to be aware of this new code, I see no problem in doing it in the code.
If you want to avoid so much if
it is possible to make a list or dictionary with the codes and make a loop. It would be 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 be something like this:
var codigos = new Dictionary {
["M50"] = "T90",
["M60"] = "T91",
["S555"] = "C100", };
If you take from a database instead of these equivalent codes is in the application code, it will be created by reading the database. You can even use the database itself directly and not create the dictionary. It would have to change something, but it works. It depends on the case. I’d probably do the cache in the dictionary. It depends on the database technology you are using, but it would be 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"];
I put in the Github for future reference.
But if you have different actions than just giving one Replace()
there the if
it will be necessary.
Put all this in the database and register in an administrative area, I think it would be the best option for you not need to keep creating new builds
– Ricardo
If you add in a file like
app.config
will also have to change always. This fits you? You do not use any database?– Jéf Bueno
can use database as well.. can give me some example?
– hrmg
You already know how to work with database?
– Jéf Bueno