.net custom business error messages

I need to implement some kind of solution, which at our level of business logic, when certain conditions are met, returns an error message.

This error message must be configured either in a file or table, which can be edited at runtime if necessary.

I saw how this was done in several ways, and it always ends with something like "This error message is {0}", and then when the developer uses the message, they don’t know how many (if any) parameters the message needs.

Just hoping to use something that may have already been done, I don’t think there is a provider or anything else within the .net framework.

+3
source share
2 answers

Decision:

Keep error messages with named placeholders, for example:

  • "This is a mistake {name1}. Andkg kfkjgkf {name2}"
  • "this is a problem {size}"

Then you need a class that accepts a message with a raw message like "this is a problem {size}" in its constructor.

Then the class will allow the developer to specify values ​​for each of the placeholders.

Finally, the developer will call a method that replaces the placeholders with the specified values ​​and returns the result.

i.e.

var rawMessage = "this is some {size} problem"; // fetch this from a file, db, or build runtime
var errorMessage = new ErrorMessage(rawMessage); // finds all the placeholders, stores them in a Dictionary<string, string>
errorMessage.SetPlaceholderValue("size", "big"); // sets the {size} placeholder value
var message = errorMessage.BuildErrorMessage(); // replaces placeholders with values and checks no values are missing

// message should look like "this is some big problem";
// this will handle any number of placeholders
0
source

( ), , , , , paramarray string.format.

, , ..

0

Source: https://habr.com/ru/post/1749521/


All Articles