BEagle1984 / silverback

Silverback is a simple but feature-rich message bus for .NET core (it currently supports Kafka, RabbitMQ and MQTT).
https://silverback-messaging.net
MIT License
257 stars 37 forks source link

Is it possible to use different DbContext for different Outbound messages to use Outbox pattern? #163

Open RomanOlegovich opened 2 years ago

RomanOlegovich commented 2 years ago

The application is designed as a modular monolith. Where each module communicates with another via kafka. Therefore, there are multiple DbContexts in one project. I want to use the Outbox pattern, but the problem is that can only specify 1 DbContext image

BEagle1984 commented 2 years ago

It's currently not possible.

The configuration will become more flexible in Silverback 4 and allow for this scenario (sneak-peak: https://github.com/BEagle1984/silverback/blob/release/4.0.0/tests/Silverback.Integration.Tests.E2E/Kafka/OutboxFixture.cs#L56) but we are still a bit far from the delivery date (which I cannot even estimate at the moment).

In your case, it would probably make sense to have a DbContext dedicated to the Silverback tables and use that across all your modules/features. Then you can use an ambient transaction (using the TransactionScope) to ensure the transactionality between the contexts (not all drivers support the ambient transactions, but it's not an issue since you seem to use SqlServer).

RomanOlegovich commented 2 years ago

Thank you for answer. Unfortunately, TransactionScope makes impossible to use different databases in different modules.

BEagle1984 commented 2 years ago

Why?

RomanOlegovich commented 2 years ago

As you say "not all drivers support the ambient transactions"

RomanOlegovich commented 2 years ago

Does silverback support TransactionScope?

BEagle1984 commented 2 years ago

The transaction is only a database thing, Silverback supports what the underlying database driver library supports. If you use MSSQL, it surely works. If you use something different just have a look at the driver's documentation to know if it's supported.