Another common approach to communicating between modules is messaging. By publishing and handling messages, a module can perform an operation when an event happens in another module.
## Understanding the Event Bus Types
ABP provides two types of event buses for loosely coupled communication:
* [Local Event Bus](../../framework/infrastructure/event-bus/local/index.md) is suitable for in-process messaging. Since in a modular monolith, both of publisher and subscriber are in the same process, they can communicate in-process, without needing an external message broker.
@ -51,30 +53,27 @@ public class OrderPlacedEto
}
````
`OrderPlacedEto` is very simple. It is a plain C# class used to transfer data related to the event (*ETO* is an acronym for *Event Transfer Object*, a suggested naming convention but not required). You can add more properties if needed, but for this tutorial, it is more than enough.
`OrderPlacedEto` is very simple. It is a plain C# class used to transfer data related to the event (*ETO* is an acronym for *Event Transfer Object*, a suggested naming convention by the ABP team, but not technically required). You can add more properties if needed, but for this tutorial, that is more than enough.
### Using the `IDistributedEventBus` Service
The `IDistributedEventBus` service publishes events to the event bus. Until this point, the Ordering module has no functionality to create new orders. Let's change that and place an order, for that purpose open the `ModularCrm.Ordering` module's .NET solution, and update the `OrderAppService` as follows:
The `IDistributedEventBus` service publishes events to the event bus. Open the `ModularCrm.Ordering` module's .NET solution, and update the `OrderAppService` as follows:
````csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ModularCrm.Ordering.Contracts.Enums;
using ModularCrm.Ordering.Events;
using ModularCrm.Ordering.Entities;
using ModularCrm.Products.Integration;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;
using ModularCrm.Ordering.Contracts.Services;
namespace ModularCrm.Ordering.Services;
public class OrderAppService : OrderingAppService, IOrderAppService
Here, select the `ModularCrm.Ordering.Contracts` package on the left side (because we want to add that package reference) and `ModularCrm.Catalog` package on the middle area (because we want to add the package reference to that project). Also, select the `ModularCrm.Ordering` package on the right side, and unselect all packages on the middle area (we don't need the implementation or any other packages). Then, click the OK button to finish the installation operation.
You can check the ABP Studio's *Solution Explorer* panel to see the module import and the project reference (dependency).
@ -166,7 +165,7 @@ Now, it is possible to use the `OrderPlacedEto` class inside the Catalog module
Open the Catalog module's .NET solution in your IDE, locate the `ModularCrm.Catalog` project, and create a new `Orders` folder and an `OrderEventHandler` class inside that folder. The final folder structure should be like this:
Replace the `OrderEventHandler.cs` file's content with the following code block:
@ -214,7 +213,7 @@ public class OrderEventHandler :
We inject the product repository and update the stock count in the event handler method (`HandleEventAsync`). That's it.
### Testing the Order Creation
## Testing the Order Creation
To keep this tutorial more focused, we will not create a UI for creating an order. You can easily create a form to create an order on your user interface. In this section, we will test it just using the Swagger UI.
@ -233,7 +232,7 @@ Find the *Orders* API, click the *Try it out* button, enter a sample value the t
}
````
> **IMPORTANT:** Here, you should type a valid Product Id from the Products table of your database!
> **IMPORTANT:** Here, you should type a valid product Id from the *CatalogProducts* table of your database!
Once you press the *Execute* button, a new order is created. At that point, you can check the `/Orders` page to see if the new order is shown on the UI, and check the `/Products` page to see if the related product's stock count has decreased.
@ -242,3 +241,7 @@ Here are sample screenshots from the Products and Orders pages:
We placed a new order for Product C. As a result, Product C's stock count has decreased from 55 to 54 and a new line is added to the Orders page.
## Conclusion
In this part, we've used ABP's distributed event bus to perform loosely coupled messaging between the modules. In the [next part](part-08.md), we will execute a database query that includes product and order data as an alternative way of integrating modules' data.