商务舱设置留言箱有错吗?

发布于 2024-07-29 19:45:18 字数 150 浏览 5 评论 0原文

在业务类中引用 System.Windows.Forms 并使用 MessageBox.Show 是否错误?

目前有一个事件处理装饰器类装饰服务类。 当某些事件触发时,装饰器会询问用户是否要继续处理某些功能。

这个装饰器类可以有这些消息框吗?

Is having reference to System.Windows.Forms in a business class and using MessageBox.Show wrong?

Currently have a event processing decorator class decorating a service class. When certain events fired decorator would like to ask user if they want to proceed processing certain functionality.

Is it ok for this decorator class have these message boxes?

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

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

发布评论

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

评论(3

晨光如昨 2024-08-05 19:45:18

您永远不应该在商务舱中使用 UI。

原因是您永远不知道您的商务舱将来会如何使用。 也许它将用在新网站、Web 服务、Windows 服务等中。在所有这些情况下,消息框都是不合适的。

处理此问题的正确方法是提供一个事件,您的 UI 或您的业务类的任何其他使用者都可以订阅该事件。 让 UI 层决定是否显示消息框。

您当然还应该查看一些日志框架,并可能记录此事件。

You should never have a UI in a business class.

The reason for this is you never know how your business class might be used down the road. Perhaps it will be used in a new website, a web service, a windows service, etc. In all of those cases a message box would be inappropriate.

The right way to handle this is to provide an event that your UI or any other consumer of your business class can subscribe to. Let the UI layer decide whether it will show a message box or not.

You certainly should also look at some of the logging frameworks out there and probably log this event.

望笑 2024-08-05 19:45:18

消息框可能到处都是错误的。 在你展示结果之前我就已经可以告诉你结果了。 用户将单击“取消”。 他们总是这样做。 如果您稍后点击同一个消息框,您的用户将单击“继续”,因为“取消”没有执行他们想要的操作。

所以,如果你已经知道答案,为什么还要问这个问题呢?

Message boxes are probably wrong everywhere. I can already tell you the result before you've shown them. Users will click Cancel. They always do. If you hit the same messagebox not a whole lot later, your users are going to click continue because "Cancel" didn't do what they wanted.

So, if you already know the answer, why bother asking the question?

澜川若宁 2024-08-05 19:45:18

在商务类中,您永远不应该使用任何直接 UI 通信。

这是因为 UI 可以是 winforms/webforms/console/smart_devices/etc...或者不使用 UI(例如在脚本中)。

如果您在业务流程中需要一些用户决定,您可以使用多种方法,以及如何做到这一点。 在.NET 中,事件是最简单的方法之一。

例如:

public class MyBussinesClass {
    public void DoSomeBussinesRelatedWork() {
        // ... some code and then you need a users decision
        var argWhichCurrencyToUse = new DecisionEventArgs {
           Title = "Currency selection",
           Text = "Which currency you want to use in bill?",
           Answer = "USD"
        };
        this.OnDecisionRequred( argWhichCurrencyToUse );
        // ... contine in work ...
    }

    protected void OnDecisionRequired( DecisionEventArgs e ) {
        // run the event
    }
    public event EventhHandler<DecisionEventArgs> DecisionRequired;
}

public class DecisionEventArgs {
    public string Title {get;set;}
    public string Text {get;set;}
    public object Answer {get;set;}
}

UI 然后可以挂钩事件并显示正确的 UI(消息框、输入框、Web 表单、控制台读/写等......

In bussines class you should NEVER use any direct-UI communications.

Its because the UI can be winforms/webforms/console/smart_devices/etc... or no UI is used (in scripts for example).

If you need some user-decide in bussines process you can use several ways, how to that. In .NET is one of the simple way Events.

For example:

public class MyBussinesClass {
    public void DoSomeBussinesRelatedWork() {
        // ... some code and then you need a users decision
        var argWhichCurrencyToUse = new DecisionEventArgs {
           Title = "Currency selection",
           Text = "Which currency you want to use in bill?",
           Answer = "USD"
        };
        this.OnDecisionRequred( argWhichCurrencyToUse );
        // ... contine in work ...
    }

    protected void OnDecisionRequired( DecisionEventArgs e ) {
        // run the event
    }
    public event EventhHandler<DecisionEventArgs> DecisionRequired;
}

public class DecisionEventArgs {
    public string Title {get;set;}
    public string Text {get;set;}
    public object Answer {get;set;}
}

The UI then can hook the events and show the correct UI (messagebox, inputbox, webform, console read/write, etc....

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