PostgreSQL


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

PostgreSqlSettings postgreSqlSagaStoreSettings = new("CONNECTION-STRING");

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

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

PostgreSqlSagaStoreSetup.Run(databaseMigrator, postgreSqlSagaStoreSettings.ConnectionString).Wait();

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

IMPORTANT

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