Allow new conditions to be added without modifying code

Asked

Viewed 110 times

5

I am manipulating a .txt and I have to exchange values like M31 for T90 for example.

My code is like this at the moment:

 //Change machine tools for Sodick
                        if (_strLinesFinal.Contains("M50"))
                        {
                            _OS = _OS.Replace("M50", "T90");
                        }

                        if (_strLinesFinal.Contains("M60"))
                        {
                            _OS = _OS.Replace("M60", "T91");
                        }

                        if (_strLinesFinal.Contains("S555"))
                        {
                            _OS = _OS.Replace("S555", "C100");
                        }

The problem is I’ll have to always add one if for every code I want to change. I wanted something more practical, maybe put in a app.config, for example. How can I do this without always having to add a condition for code exchange?

  • 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

  • 1

    If you add in a file like app.config will also have to change always. This fits you? You do not use any database?

  • can use database as well.. can give me some example?

  • You already know how to work with database?

1 answer

7


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.

  • good. I’ll try to do that!

  • Perfect... worked... made some modifications... but removed half of my code with the if .. thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.