Microsoft SQL Server¶
To use Microsoft SQL Server - Saga Store
integration, the NuGet package CrystalSharp.MsSql
must be installed.
IMPORTANT
Kindly keep in mind that this particular configuration is intended for the purpose of storing transaction states in the saga store. However, the configuration procedures for database integration, Event Store, and Read Model Store will be approached differently.
Saga Store Registration¶
Registration for the saga store implementation is required. Following is the code that illustrates how to register the implementation of the saga store in the Program.cs
file:
MsSqlSettings msSqlSagaStoreSettings = new("CONNECTION-STRING");
IResolver resolver = CrystalSharpAdapter.New(builder.Services)
.AddCqrs(typeof(CreateCategoryCommandHandler))
.AddMsSqlSagaStore(msSqlSagaStoreSettings, typeof(PlaceOrderTransaction))
.CreateResolver();
IMsSqlDatabaseMigrator databaseMigrator = resolver.Resolve<IMsSqlDatabaseMigrator>();
MsSqlSagaStoreSetup.Run(databaseMigrator, msSqlSagaStoreSettings.ConnectionString).Wait();
In the above code snippet, when initializing the Crystal Sharp framework, a call to an extension method is made AddMsSqlSagaStore(msSqlSagaStoreSettings, typeof(PlaceOrderTransaction))
for the saga store registration. The parameter typeof(PlaceOrderTransaction)
scans the assembly where saga transactions, saga transaction handlers, saga locators, and saga implementations reside and registers them.
After registration of the saga store, it is mandatory to run database scripts to create the tables for the saga store. The MsSqlSagaStoreSetup
class provides a Run
method that expects an interface IMsSqlDatabaseMigrator
and connection string to execute the database scripts.
IMPORTANT
It is mandatory to run “MsSqlSagaStoreSetup.Run(databaseMigrator, msSqlSagaStoreSettings.ConnectionString).Wait()” to create the tables for the saga store.