监视工作流程/状态的电子邮件通知服务的设计模式

发布于 2024-09-24 15:11:37 字数 1297 浏览 1 评论 0原文

很抱歉这个问题有点模糊,因为我不完全确定我在寻找什么。

基本上在我的(Asp.net MVC)网络应用程序中,我有一些由各种用户输入触发的基本(手动编码)工作流程。

当某些事件发生时,我需要发送电子邮件可配置通知。

例如,我的控制器中有一个更新状态方法,我可以编写如下内容:

public ActionResult UpdateStatus(int id, Status status)
{
    Orders order = _orders.GetById(id);
    order.Status = status;

    if (status = Status.AwaitingApproval)
    {
        SendAwaitingApprovalNotification(_roles.GetApproverForDept(order.Department));
    }

    else if (status = Status.Approved && order.Department = someDept) { ... }

    else if (status = Status.Approved && order.Department = someOtherDept) { ... }

    else if // ... etc
}

正如您所看到的,这一切很快就会变得混乱。而且,发送通知的时间/内容的逻辑被编码到控制器中,这对我来说并不合适。

此外,还有各种其他操作可以更改状态,而无需专门调用 UpdateStatus 方法。例如,

public ActionResult ChangeOrderCost(int id, decimal newCost)
{
    Orders order = _orders.GetById(id);
    order.Cost = newCost;
    order.Status = Status.AwaitingCustomerApproval;
}

这些只是编造的例子,但我希望您能明白。

我真正的想法是,我的所有通知规则都应该独立于某种可配置的通知类中,该类基本上监视我的对象的状态变化并执行它需要执行的任何操作,因此它的作用就像主程序的插件一样应用程序并保持松散耦合。同时,我真的不觉得我的底层数据对象应该依赖于通知服务之类的东西,我认为这应该插入到我的主控制器类中。

我真的不知道如何实现这一点,但我猜这一定是一个常见问题。我可以阅读特定的设计模式来提供一些线索吗?以前有人做过这样的事情吗?我是否正确地思考了这个问题?我对所有这些东西都很陌生,但试图以正确的方式思考和编码。

I'm sorry this question is going to be a bit vague because I'm not entirely sure what I'm looking for.

Basically in my (Asp.net MVC) web app I have some basic (manually coded) workflow triggered by my various user inputs.

I need to send email configurable notifications when certain events happen.

For example, there is an update status method in my controller, which I could write something like this:

public ActionResult UpdateStatus(int id, Status status)
{
    Orders order = _orders.GetById(id);
    order.Status = status;

    if (status = Status.AwaitingApproval)
    {
        SendAwaitingApprovalNotification(_roles.GetApproverForDept(order.Department));
    }

    else if (status = Status.Approved && order.Department = someDept) { ... }

    else if (status = Status.Approved && order.Department = someOtherDept) { ... }

    else if // ... etc
}

As you can see, this is all going to get messy very quickly. And also, the logic for when/what notifications are sent is coded into the controller, which doesn't feel right to me.

Also, there are various other actions which can change the state without specifically calling the UpdateStatus method. eg

public ActionResult ChangeOrderCost(int id, decimal newCost)
{
    Orders order = _orders.GetById(id);
    order.Cost = newCost;
    order.Status = Status.AwaitingCustomerApproval;
}

These are just made up examples but I hope you get the idea.

What I'm really thinking is that all my notification rules should be self-contained in some kind of configurable notication class, which basically monitors state changes on my object and does whatever it needs to do, so it acts like a plugin to the main application and maintains loose coupling. At the same time, I don't really feel like my underlying data objects should have dependencies on things like notification services, I kind of think this should plug-in to my main controller classes.

I can't really think how to achieve this, but I'm guessing it must be a common problem. Is there a specific design pattern I can read up on to give some clues? Has anyone done something like this before? Am I even thinking about this problem correctly? I'm pretty new to all this stuff but trying to think and code the correct way.

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

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

发布评论

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

评论(1

似狗非友 2024-10-01 15:11:37

我建议采用责任链模式:
http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern

它基本上是一个链,其中每个元素根据其实现的测试条件起作用/不起作用。这些条件可以配置,但这取决于您的实施。

I would advise Chain-Of-Responsibility pattern:
http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern

It is basically a chain in which each element acts/doesn't act based on the test condition implemented by it. These conditions can be made configurable but it is down to your implementation.

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