何时以及为何使用自定义事件处理程序

时间:2021-06-16 00:03:10

I have an object (actually a visual element and a viewmodel element) which I'd like to use in multiple places throughout my project. The function of the object is to collect payment information. Customer Smith ordered a Widget from my company, but he's late with the payment. He calls me to say that he'll pay the amount in full on the 1st. I fill in the payment amount and date, click a button, and an entry is added to my database with Mr. Smith's proposed payment info.

我有一个对象(实际上是一个可视元素和一个viewmodel元素),我想在整个项目的多个地方使用它。对象的功能是收集支付信息。顾客史密斯从我的公司订购了一个小工具,但他付款的时间已经很晚了。他打电话给我说他将在1日全额支付金额。我填写了付款金额和日期,点击一个按钮,并根据史密斯先生建议的付款信息将条目添加到我的数据库中。

Now, as I said, I want to use this object in multiple places, and depending on where I'm using it, I may want more to happen after clicking the submit button. Along with Widgets, I also sell Whatsits and Whoozits. After receiving payment info for a Whatsit, I want to populate an additional table which will send the customer a reminder. After entering Whoozit payment info, I want to populate the reminder table, as well immediately sending the customer an SMS confirmation.

现在,正如我所说,我想在多个地方使用这个对象,并且根据我使用它的位置,我可能希望在单击提交按钮后发生更多。与Widgets一起,我还出售Whatsits和Whoozits。在收到Whatsit的付款信息后,我想填充一个额外的表格,该表格将向客户发送提醒。输入Whoozit付款信息后,我想填写提醒表,并立即向客户发送短信确认。

My original idea was simply to add a List<Action> field to my original object, and depending on where I'm accessing it from, send it any additional actions I want done after the original database update is complete.

我最初的想法是简单地将List 字段添加到我的原始对象,并根据我从哪里访问它,在原始数据库更新完成后发送我想要完成的任何其他操作。

Something like this:

像这样的东西:

public class MoneyGetter
{
    public List<Action> AfterEvents = new List<Action>();

    public void SavePaymentInfo()
    {
        //logic for initial database entry

        foreach(var a in AfterEvents)
        {
            a.Invoke();
        }
    }
}

And after creating the object, depending on where I'm using it, I'd simply add

创建对象后,根据我使用它的位置,我只需添加

var mg = new MoneyGetter();
mg.AfterEvents.Add(() => { /* add situation specific action logic here */ });

After getting through some of this, it occurred to me that this is what events are for (I think), and I should be maybe creating a custom OnSubmitted event for my MoneyGetter object, subscribing to that event from whatever object is using it, and raising the event after the initial DB entry.

在完成其中一些之后,我想到这就是事件的目的(我认为),我应该为我的MoneyGetter对象创建一个自定义OnSubmitted事件,从任何使用它的对象订阅该事件,并且在初始数据库输入后引发事件。

Is that the case? And if so, why is going through the trouble of creating a custom event handler a better idea than just having a List<Action>?

是这样的吗?如果是这样,为什么要创建一个自定义事件处理程序的麻烦比一个List 更好?

2 个解决方案

#1


0  

Is that the case?

是这样的吗?

Probably, yes. If you want to be able to notify any subscribers when something happens in your class, raising an event is a common and good approach.

可能是。如果您希望能够在课堂上发生任何事情时通知任何订阅者,举办活动是一种常见而且好的方法。

And if so, why is going through the trouble of creating a custom event handler a better idea than just having a List<Action>?

如果是这样,为什么要创建一个自定义事件处理程序的麻烦比一个List 更好?

Well, adding Action<T>s to a public List<T> is kind of a convoluted way to implement events. But I guess it would actually work. As a consumer of your class, I would expcect to be able to subscribe to an event using the += syntax though instead of having to add/remove actions from a publicly exposed List<T>.

好吧,将Action 添加到公共List 是一种实现事件的复杂方式。但我想它确实会奏效。作为您的类的使用者,我会说明能够使用+ =语法订阅事件,而不必从公开的List 添加/删除操作。

#2


0  

What about the event aggregator pattern? I use this quite frequently when my viewmodels need to communicate between each other.

事件聚合器模式怎么样?当我的视图模型需要在彼此之间进行通信时,我经常使用它。

Caliburn.Micro has a light-weight one built in that you could use. If you use an IoC container then its a snap to ensure every object can get a reference to the event aggregator and communicate freely.

Caliburn.Micro有一个内置的轻量级,你可以使用。如果您使用IoC容器,那么它就是一个快照,以确保每个对象都可以获得对事件聚合器的引用并*地进行通信。

The Event Aggregator

事件聚合器

Caliburn.Micro comes pre-bundled with an Event Aggregator, conveniently called EventAggregator. For those unfamiliar, an Event Aggregator is a service that provides the ability to publish an object from one entity to another in a loosely based fashion. Event Aggregator is actually a pattern and it's implementation can vary from framework to framework. For Caliburn.Micro we focused on making our Event Aggregator implementation simple to use without sacrificing features or flexibility. Getting Started

Caliburn.Micro预先捆绑了一个Event Aggregator,方便地称为EventAggregator。对于那些不熟悉的人,Event Aggregator是一种服务,它提供了以一种基于松散的方式将对象从一个实体发布到另一个实体的能力。 Event Aggregator实际上是一种模式,它的实现可能因框架而异。对于Caliburn.Micro,我们专注于使我们的Event Aggregator实现易于使用而不牺牲功能或灵活性。入门

As previously mentioned we provide an implementation of Event Aggregator for you. This implementation implements the IEventAggregator interface, however, you can provide your own implementation if required. Please take a moment to familiarize yourself with the IEventAggregator signature.

如前所述,我们为您提供了Event Aggregator的实现。此实现实现了IEventAggregator接口,但是,如果需要,您可以提供自己的实现。请花点时间熟悉IEventAggregator签名。

public interface IEventAggregator {
  bool HandlerExistsFor(Type messageType);
  void Subscribe(object subscriber);
  void Unsubscribe(object subscriber);
  void Publish(object message, Action<Action> marshal);
} 

Info from: https://caliburnmicro.codeplex.com/wikipage?title=The%20Event%20Aggregator

来自的信息:https://caliburnmicro.codeplex.com/wikipage?title = The%20Event%20Aggregator

#1


0  

Is that the case?

是这样的吗?

Probably, yes. If you want to be able to notify any subscribers when something happens in your class, raising an event is a common and good approach.

可能是。如果您希望能够在课堂上发生任何事情时通知任何订阅者,举办活动是一种常见而且好的方法。

And if so, why is going through the trouble of creating a custom event handler a better idea than just having a List<Action>?

如果是这样,为什么要创建一个自定义事件处理程序的麻烦比一个List 更好?

Well, adding Action<T>s to a public List<T> is kind of a convoluted way to implement events. But I guess it would actually work. As a consumer of your class, I would expcect to be able to subscribe to an event using the += syntax though instead of having to add/remove actions from a publicly exposed List<T>.

好吧,将Action 添加到公共List 是一种实现事件的复杂方式。但我想它确实会奏效。作为您的类的使用者,我会说明能够使用+ =语法订阅事件,而不必从公开的List 添加/删除操作。

#2


0  

What about the event aggregator pattern? I use this quite frequently when my viewmodels need to communicate between each other.

事件聚合器模式怎么样?当我的视图模型需要在彼此之间进行通信时,我经常使用它。

Caliburn.Micro has a light-weight one built in that you could use. If you use an IoC container then its a snap to ensure every object can get a reference to the event aggregator and communicate freely.

Caliburn.Micro有一个内置的轻量级,你可以使用。如果您使用IoC容器,那么它就是一个快照,以确保每个对象都可以获得对事件聚合器的引用并*地进行通信。

The Event Aggregator

事件聚合器

Caliburn.Micro comes pre-bundled with an Event Aggregator, conveniently called EventAggregator. For those unfamiliar, an Event Aggregator is a service that provides the ability to publish an object from one entity to another in a loosely based fashion. Event Aggregator is actually a pattern and it's implementation can vary from framework to framework. For Caliburn.Micro we focused on making our Event Aggregator implementation simple to use without sacrificing features or flexibility. Getting Started

Caliburn.Micro预先捆绑了一个Event Aggregator,方便地称为EventAggregator。对于那些不熟悉的人,Event Aggregator是一种服务,它提供了以一种基于松散的方式将对象从一个实体发布到另一个实体的能力。 Event Aggregator实际上是一种模式,它的实现可能因框架而异。对于Caliburn.Micro,我们专注于使我们的Event Aggregator实现易于使用而不牺牲功能或灵活性。入门

As previously mentioned we provide an implementation of Event Aggregator for you. This implementation implements the IEventAggregator interface, however, you can provide your own implementation if required. Please take a moment to familiarize yourself with the IEventAggregator signature.

如前所述,我们为您提供了Event Aggregator的实现。此实现实现了IEventAggregator接口,但是,如果需要,您可以提供自己的实现。请花点时间熟悉IEventAggregator签名。

public interface IEventAggregator {
  bool HandlerExistsFor(Type messageType);
  void Subscribe(object subscriber);
  void Unsubscribe(object subscriber);
  void Publish(object message, Action<Action> marshal);
} 

Info from: https://caliburnmicro.codeplex.com/wikipage?title=The%20Event%20Aggregator

来自的信息:https://caliburnmicro.codeplex.com/wikipage?title = The%20Event%20Aggregator