debugcsharpMinor
Generic error message factory
Viewed 0 times
factorygenericmessageerror
Problem
The purpose of this code is to centralize all error / status messages to present to the user. For example, registering an account and the user email address is already registered. A status code is set then sent off to get a friendly error message to present to the user. All services will have its own set of status code and status messages which is handled by a status handler for filtering. While this is only a basic class I want to make sure that I am going in the right direction before extending.
Status codes
List of statuses for registration
Message status handler
Again, it is a
Status codes
namespace Services.RegistrationService
{
public enum StatusCodes
{
UserAlreadyExists,
// more to come
}
}List of statuses for registration
using Framework;
using System.Collections.Generic;
namespace Services.RegistrationService
{
public class RegistrationStatusMessages : MessageStatusHandler
{
public void AddErrors(List codes)
{
foreach(var errorCodes in codes)
switch (errorCodes)
{
case StatusCodes.UserAlreadyExists: { base.add("User already exists", "EmailAddress", true); break; }
}
}
}
}Message status handler
using System.Collections.Generic;
using System.Linq;
namespace Framework
{
public class MessageStatusHandler
{
protected List _errorList;
private string _friendlyErrorMessage;
private bool _isError;
private string _propertyName;
public MessageStatusHandler()
{
_errorList = new List();
}
protected void add(string friendlyErrorMessage, string propertyName, bool isError)
{
_errorList.Add(new MessageStatusHandler { _friendlyErrorMessage = friendlyErrorMessage, _propertyName = propertyName, _isError = isError });
}
public bool HasErrors()
{
return _errorList.Any(x => x._isError == true);
}
}
}Again, it is a
Solution
Interesting idea, much better than having status codes / messages / magic strings wandering everywhere. This is also a good starting point for making messages loclaziable - if they are centralized, the can also be taken from resources in centralized manner.
Regarding possible improvements:
Regarding possible improvements:
- I would consider changing
AddErrors(List codes)toAddErrors(params StatusCodes[] codes)- it will be easier to add just one error (what I suppose is most probable scenrio) or many with sam emethod without need to construct the List object.
- It might be interesting to add some events to the Manager class - this way some important Listeneres might be configured, i.e. when status
Unauthorizedappears some listener just cancels all current responses and redirects user to error / login page. OrDatabaseNotRespondingmight have listener attached that counts occurences per minute and when there is more than one or two (what can happen in heavilly trafficked network) reports this immediatelly to the Admin. These two examples might not be a best design anyway - but I just point the purpose.
- Probably you are thought about this already, but I would definitelly divide
StatusCodesinto specialized classes for ease of use likeSystemStatusCodes,UserStatusCodes,OrderStatusCodesetc...
- I think there is some error in your code in class
MessageStatusHandler:protected List _errorList;. I don't see a point in creating collection of handlers inside on of the exact handler type, perhaps you thought about gathering more results in one object - but I think this part needs more designing that just that simple exmaple.
Context
StackExchange Code Review Q#61823, answer score: 7
Revisions (0)
No revisions yet.