Is this WPF error handling a good idea?
I have a multi threaded wpf application with various HW interfaces.
I want to react to several HW failures that can happen.
For example :
one of the interfaces is a temperature sensor and i want that from a certain temp. a meesage would appear and notify the user that it happened.
i came up with the follwing design :
/// <summary>
/// This logic reacts to errors that occur during the system run.
/// The reaction is set by the component that raised the error.
/// </summary>
public class ErrorHandlingLogic : Logic
{
}
the above class would consume ErrorEventData that holds all the information about the error that occurred.
public class ErrorEventData : IEventData
{
#region public enum
public enum ErrorReaction
{
}
#endregion public enum
#region Private Data Memebers and props
private ErrorReaction m_ErrorReaction;
public ErrorReaction ErrorReactionValue
{
get开发者_运维知识库 { return m_ErrorReaction; }
set { m_ErrorReaction = value; }
}
private string m_Msg;
public string Msg
{
get { return m_Msg; }
set { m_Msg = value; }
}
private string m_ComponentName;
public string ComponentName
{
get { return m_ComponentName; }
set { m_ComponentName = value; }
}
#endregion Private Data Memebers and props
public ErrorEventData(ErrorReaction reaction, string msg, string componenetName)
{
m_ErrorReaction = reaction;
m_Msg = msg;
m_ComponentName = componenetName;
}
}
the above ErrorHandlingLogic would decide what to do with the ErrorEventData sent to him from various components of the application.
if needed it would be forwarded to the GUI to display a message to the user.
so what do you think is it a good design ?
thanks, Adiel.
It seems fair enough, however, in terms of design I would have probably just went with a standard Event with custom event args.
Here is an example:
public interface IEventData
{
ErrorReaction Reaction { get; }
string Message { get; }
ComponentName { get; }
}
public class HardwareChangeEventData : IEventData
{
public HardwareChangeEventData(ErrorReaction reaction, string msg, string componentName)
{
Reaction = reaction;
Message = msg;
ComponentName = componentName;
}
public ErrorReaction Reaction { get; private set; }
public string Message { get; private set; }
public ComponentName { get; private set; }
}
....
// introduce a base class so all hardware components can raise the event
public class HardwareComponent
{
public delegate void HardwareChangedEventHandler(IEventData ed);
public event HardwareChangedEventHandler HardwareChanged;
//event-invoking method that derived classes can override.
protected virtual void OnHardwareChanged(IEventData ed)
{
HardwareChangedEventHandler handler = HardwareChanged;
if (handler != null)
{
handler(this, ed);
}
}
}
public class TemperatureGauge : HardwareComponent
{
public void Monitor()
{
// example logic
while (...)
{
if (Temperature < LowThreshold)
{
IEventData ed = new HardwareChangeEventData(ErrorReaction.IncreaseTemp, "Temperature too low!", "TemperatureGauge");
OnHardwareChanged(ed);
}
}
}
public override OnHardwareChanged(IEventData ed)
{
// do something with ed internally (if applicable)
// forward event on to base so it can be passed out to subscribers
base.OnHardwareChanged(ed);
}
}
Above code looks fine.
But for notifying different components , i would say look for Observer pattern ( Event/Deleagte)
if you are going to handle error in WPF why don't use validators for that? see this acticle
精彩评论