这个 WPF 错误处理是个好主意吗?

发布于 2024-10-10 05:00:39 字数 1641 浏览 0 评论 0原文

我有一个具有各种硬件接口的多线程 wpf 应用程序。

我想对可能发生的几个硬件故障做出反应。

例如:

其中一个接口是温度传感器,我希望在某个温度下得到它。将出现一条消息并通知用户发生了这种情况。

我提出了以下设计:

/// <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
{

}

上面的类将使用 ErrorEventData 来保存有关发生的错误的所有信息。

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;
        }
    }

上面的 ErrorHandlingLogic 将决定如何处理从应用程序的各个组件发送给他的 ErrorEventData。

如果需要,它将被转发到 GUI 以向用户显示消息。

那么您认为怎样的设计才是好的设计呢?

谢谢, 阿迪尔。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花海 2024-10-17 05:00:39

然而,这似乎很公平,就设计而言,我可能会选择带有自定义事件参数的标准事件。

这是一个例子:

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);
    }
}

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);
    }
}
╰ゝ天使的微笑 2024-10-17 05:00:39

上面的代码看起来不错。

但是为了通知不同的组件,我会说寻找观察者模式(事件/委托)

Above code looks fine.

But for notifying different components , i would say look for Observer pattern ( Event/Deleagte)

哎呦我呸! 2024-10-17 05:00:39

如果要处理 WPF 中的错误,为什么不使用验证器呢? 查看这篇文章

if you are going to handle error in WPF why don't use validators for that? see this acticle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文