Using Events Delegates in multi tenant web application

前端 未结 2 919
时光取名叫无心
时光取名叫无心 2021-01-21 19:36

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

In my service layer I am defining custom events for every important action and raising these e

相关标签:
2条回答
  • 2021-01-21 19:56

    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.

    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.

    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.

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

    0 讨论(0)
  • 2021-01-21 20:23

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

    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

    0 讨论(0)
提交回复
热议问题