views:

53

answers:

2

Hi all,

I need to implement some kind of solution such that in our business logic layer when certain conditions are met an error message is returned.

That error message should be configurable either in a file or table that can be edited at run time if needed.

I've seen it done before a few ways and it always ends up something like "This error message is {0}" and then when the dev goes the use the message they dont neccesarily know how many (if any) parameters the message needs.

Just hoping to leverage off something that may have already been done, I dont think there is a provider or anything already in the .net framework.

A: 

why not make the error message a property of your error class (assuming you have one), allowing your developers to set their own messages, perhaps event including a few static messages of your own, and then accepting a paramarray to put in as the second part of your string.format function.

seems like that would get around the lack of knowing how many parameters, etc.

nathan gonzalez
The main thing I want to try and accomplish is that all error messages are standardized in the application so I'd like a developer only to be able to choose from a certain number of predefined error messages.I suppose I could have an error message enum that defines the available types and in the error class set the enum to the type of error and have the dev provide the required params
Daniel
yeah, i mean, at some point it's the developers responsibility to know how to report errors. make your code as bulletproof as possible, throwing "good" errors when the dev isn't providing enough/correct information, and call it a day. at some point you have to trust people to do their job, ya know. :)
nathan gonzalez
also, enum will be tough, as it won't hold anything but an int value. if i were you i'd create an interface/abstract class with ErrorMessage and FieldsRequired properties, and then inherit from those, and force a dev to pass in a message that inherits from your class. that way you could check the paramarray length against the requirements of the selected message
nathan gonzalez
A: 

A solution;

Store your error messages with named placeholders like this;

  • "This is an error {name1}. Andkg kfkjgkf {name2}"
  • "this is some {size} problem"

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

The class would then allow the developer to specify values for each of the placeholders.

Finally the developer would 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
Casey Burns