在多租户web应用程序中使用事件委托

时间:2021-12-15 12:46:30

I'm developing a multi tenant n-tier web application using asp.net Mvc 5.

我正在开发一个使用asp.net Mvc 5的多租户n层web应用程序。

In my service layer I am defining custom events for every important action and raising these events once these actions are executed. For example

在我的服务层中,我为每一个重要的操作定义自定义事件,并在执行这些操作后引发这些事件。例如

Public event EventHandler EntityCreated;

Public void Create(Entity item) {
  Save(item);
  ......
  EntityCreated(this, item);
}

I intend on hooking up business rules and notifications to these events. The main reason I want to use events is decoupling of the logic and easy plug-ability of more events handlers without modifying my service layer.

我打算将业务规则和通知连接到这些事件。我使用事件的主要原因是不需要修改我的服务层,就可以解耦更多事件处理程序的逻辑和易于插入的能力。

Question: Does it make sense using events and delegates in asp.net?

问:在asp.net中使用事件和委托是否有意义?

Most examples I find online are for win forms or wpf. I get the advantage when it comes to multithreaded applications. Also the events are defined once per form and are active for the lifetime of the form.

我在网上找到的大多数例子都是win forms或wpf。在多线程应用程序中,我获得了优势。同样,每个窗体定义一次事件,并在窗体的生命周期中活动。

But in my case the events will be per http request. So is it an overhead defining these events?

但在我的情况下,事件将是每个http请求。那么,它是定义这些事件的开销吗?

2 个解决方案

#1


2  

As others pointed out that pub/sub or event bus is one solution. Another solution is something like what you are trying to do here but make it more formal.

正如其他人指出的那样,pub/sub或event bus是一种解决方案。另一个解决方案是类似于你在这里尝试做的事情,但是要使它更加正式。

Let's take a specific example of creating a customer. You want to send a welcome email when a new customer is created in the application. The domain should only be concerned with creating the customer and saving it in the db and not all the other details such as sending emails. So you add a CustomerCreated event. These types of events are called Domain Event as opposed to user interface events such as button click etc.

让我们举一个创建客户的具体例子。当在应用程序中创建新客户时,您希望发送一封欢迎电子邮件。域应该只关心创建客户并将其保存在数据库中,而不是所有其他细节,比如发送电子邮件。添加customercreate事件。这些类型的事件被称为域事件,而不是像按钮单击之类的用户界面事件。

When the CustomerCreated event is raised, it should be handled somewhere in the code so that it can do the needful. You can use an EventHandlerService as you mentioned (but this can soon becomes concerned with too many events) or use the pattern that Udi Dahan talks about. I have successfully used Udi's method with many DI Containers and the beauty of the pattern is that your classes remain SRP compliant. You just have to implement a particular interface and registration code at the application bootstrap time using reflection.

当引发customercreate事件时,应该在代码中的某个地方处理它,以便它能够执行所需的操作。正如您所提到的,您可以使用EventHandlerService(但这很快就会涉及到太多事件),或者使用Udi Dahan所说的模式。我已经在许多DI容器中成功地使用了Udi的方法,这个模式的好处是您的类保持SRP兼容。您只需使用反射在应用程序引导时实现特定的接口和注册代码。

If you need further help with this topic, let me know and I can share with you the code snippets to make it work.

如果您在这个主题上需要进一步的帮助,请让我知道,我可以与您分享代码片段以使其工作。

#2


0  

I have implemented Udi Dahan's implementation as pointed out by @Imran but with a few changes.

我已经实现了Udi Dahan的实现,正如@Imran所指出的,但是有一些改变。

My Events are being raised in a Service Layer and for that using a Static Class dint seem right. Also have added support for async/await.

我的事件在服务层中引发,因此使用静态类属性似乎是正确的。还增加了对异步/等待的支持。

Also going down the Events & Delegates path did work out but it just felt like an overhead to register the events per request.

同样,在事件和委托路径上也进行了工作,但它感觉就像一个开销来注册每个请求的事件。

I have blogged my solution here http://www.teknorix.com/event-driven-programming-in-asp-net

我已经在http://www.teknorix.com/event-drive -programming-in-asp网站上发布了我的解决方案

#1


2  

As others pointed out that pub/sub or event bus is one solution. Another solution is something like what you are trying to do here but make it more formal.

正如其他人指出的那样,pub/sub或event bus是一种解决方案。另一个解决方案是类似于你在这里尝试做的事情,但是要使它更加正式。

Let's take a specific example of creating a customer. You want to send a welcome email when a new customer is created in the application. The domain should only be concerned with creating the customer and saving it in the db and not all the other details such as sending emails. So you add a CustomerCreated event. These types of events are called Domain Event as opposed to user interface events such as button click etc.

让我们举一个创建客户的具体例子。当在应用程序中创建新客户时,您希望发送一封欢迎电子邮件。域应该只关心创建客户并将其保存在数据库中,而不是所有其他细节,比如发送电子邮件。添加customercreate事件。这些类型的事件被称为域事件,而不是像按钮单击之类的用户界面事件。

When the CustomerCreated event is raised, it should be handled somewhere in the code so that it can do the needful. You can use an EventHandlerService as you mentioned (but this can soon becomes concerned with too many events) or use the pattern that Udi Dahan talks about. I have successfully used Udi's method with many DI Containers and the beauty of the pattern is that your classes remain SRP compliant. You just have to implement a particular interface and registration code at the application bootstrap time using reflection.

当引发customercreate事件时,应该在代码中的某个地方处理它,以便它能够执行所需的操作。正如您所提到的,您可以使用EventHandlerService(但这很快就会涉及到太多事件),或者使用Udi Dahan所说的模式。我已经在许多DI容器中成功地使用了Udi的方法,这个模式的好处是您的类保持SRP兼容。您只需使用反射在应用程序引导时实现特定的接口和注册代码。

If you need further help with this topic, let me know and I can share with you the code snippets to make it work.

如果您在这个主题上需要进一步的帮助,请让我知道,我可以与您分享代码片段以使其工作。

#2


0  

I have implemented Udi Dahan's implementation as pointed out by @Imran but with a few changes.

我已经实现了Udi Dahan的实现,正如@Imran所指出的,但是有一些改变。

My Events are being raised in a Service Layer and for that using a Static Class dint seem right. Also have added support for async/await.

我的事件在服务层中引发,因此使用静态类属性似乎是正确的。还增加了对异步/等待的支持。

Also going down the Events & Delegates path did work out but it just felt like an overhead to register the events per request.

同样,在事件和委托路径上也进行了工作,但它感觉就像一个开销来注册每个请求的事件。

I have blogged my solution here http://www.teknorix.com/event-driven-programming-in-asp-net

我已经在http://www.teknorix.com/event-drive -programming-in-asp网站上发布了我的解决方案