MySQL


To use MySQL - Saga Store integration, the NuGet package CrystalSharp.MySql 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:

IMPORTANT

It is crucial to highlight that the AllowUserVariables=True setting must be enabled in the MySQL connection string.

MySqlSettings mySqlSagaStoreSettings = new("CONNECTION-STRING");

IResolver resolver = CrystalSharpAdapter.New(builder.Services)
    .AddCqrs(typeof(CreateCategoryCommandHandler))
    .AddMySqlSagaStore(mySqlSagaStoreSettings, typeof(PlaceOrderTransaction))
    .CreateResolver();

IMySqlDatabaseMigrator databaseMigrator = resolver.Resolve<IMySqlDatabaseMigrator>();

MySqlSagaStoreSetup.Run(databaseMigrator, mySqlSagaStoreSettings.ConnectionString).Wait();

In the above code snippet, when initializing the Crystal Sharp framework, a call to an extension method is made AddMySqlSagaStore(mySqlSagaStoreSettings, 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 MySqlSagaStoreSetup class provides a Run method that expects an interface IMySqlDatabaseMigrator and connection string to execute the database scripts.

IMPORTANT

It is mandatory to run “MySqlSagaStoreSetup.Run(databaseMigrator, mySqlSagaStoreSettings.ConnectionString).Wait()” to create the tables for the saga store.