Skip to content

Commit

Permalink
Add events README (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: Artem Leshchev <[email protected]>
  • Loading branch information
aleshchev and Artem Leshchev authored May 19, 2024
1 parent 4d1d902 commit dd6d30b
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ This repository offers a wide collection of .NET packages for use in microservic
- [Logging](#Logging)
- [API Middlewares](#Middlewares)
- [API Documentation](#Documentation)
- [Domain Events](#Domain-Events)
- [Integration Events](#Integration-Events)
- [Kubernetes Insights](#Insights)
- [Kubernetes Health Checks](#Health-Checks)
- [NHibernate](#NHibernate)
Expand Down Expand Up @@ -129,6 +131,104 @@ app
.UsePlatformApiDocumentation(builder.Environment);
```

## Events
To use events, first install the [NuGet package](https://www.nuget.org/packages/Luxoft.Bss.Platform.Events):
```shell
dotnet add package Luxoft.Bss.Platform.Events
```

### Domain Events

To use domain events, you need register it in DI
```C#
services
.AddPlatformDomainEvents();
```
Now, you can publish it in this way
```C#
public class CommandHandler(IDomainEventPublisher eventPublisher) : IRequestHandler<Command>
{
public async Task Handle(Command request, CancellationToken cancellationToken)
{
await eventPublisher.PublishAsync(new DomainEvent(), cancellationToken);
}
}
```

And process it in this way
```C#
public class EventHandler : INotificationHandler<DomainEvent>
{
public async Task Handle(DomainEvent notification, CancellationToken cancellationToken)
{
// your logic
}
}
```

### Integration Events
In the first step, you need implement interface IIntegrationEventProcessor
```C#
public class IntegrationEventProcessor : IIntegrationEventProcessor
{
public Task ProcessAsync(IIntegrationEvent @event, CancellationToken cancellationToken)
{
// write your consuming logic here
}
}
```

Then, register integration events in DI
```C#
services
.AddPlatformIntegrationEvents<IntegrationEventProcessor>(
typeof(IntegrationEvents).Assembly,
x =>
{
x.SqlServer.ConnectionString = "ms sql connection string";

x.MessageQueue.ExchangeName = "integration.events";
x.MessageQueue.Host = "RabbitMQ host";
x.MessageQueue.Port = "RabbitMQ port";
x.MessageQueue.VirtualHost = "RabbitMQ virtual host";
x.MessageQueue.UserName = "RabbitMQ username";
x.MessageQueue.Secret = "RabbitMQ secret";
});
```

Now, you can publish it in this way
```C#
public class CommandHandler(IIntegrationEventPublisher eventPublisher) : IRequestHandler<Command>
{
public async Task Handle(Command request, CancellationToken cancellationToken)
{
await eventPublisher.PublishAsync(new IntegrationEvent(), cancellationToken);
}
}
```

And process it in this way
```C#
public class EventHandler : INotificationHandler<IntegrationEvent>
{
public async Task Handle(IntegrationEvent notification, CancellationToken cancellationToken)
{
// your logic
}
}
```

Additional options

| Option | Description | Type | Default value |
| ----------------------- | ------------------------------------------------------------------------------------- | ------ | --------------|
| **DashboardPath** | Dashboard relative path | string | /admin/events |
| **FailedRetryCount** | The number of message retries | int | 5 |
| **RetentionDays** | Success message live period | int | 15 |
| **SqlServer.Schema** | Shema name for event tables | string | events |
| **MessageQueue.Enable** | For developer purpose only. If false, then switches RabbitMQ queue to queue in memory | string | true |
| |

## Kubernetes

To use kubernetes utils, first install the [NuGet package](https://www.nuget.org/packages/Luxoft.Bss.Platform.Kubernetes):
Expand Down

0 comments on commit dd6d30b

Please sign in to comment.