NHibernate Configuration
NHibernate is a widely used ORM and it is supported by MassTransit for saga storage. The example below shows the code-first approach to using NHibernate for saga persistence.
public class OrderState : SagaStateMachineInstance{ public Guid CorrelationId { get; set; } public string CurrentState { get; set; }
public DateTime? OrderDate { get; set; }
// If using Optimistic concurrency, this property is required public int Version { get; set; }}Create the saga instance class map
Section titled “Create the saga instance class map”The instance properties are configured using a SagaClassMapping.
public class OrderStateMap : SagaClassMapping<OrderState>{ public OrderStateMap() { Property(x => x.CurrentState, x => x.Length(64)); Property(x => x.OrderDate);
Property(x => x.Version); // If using Optimistic concurrency }}Configure the saga repository
Section titled “Configure the saga repository”To configure NHibernate as the saga repository for a saga, use the code shown below using the AddMassTransit container extension. This will configure NHibernate to connect to the local NHibernate instance on the default port using Optimistic concurrency.
// the session factory should be registered as a single instancecontainer.RegisterSingleInstance<ISessionFactory>(...);
container.AddMassTransit(cfg =>{ cfg.AddSagaStateMachine<OrderStateMachine, OrderState>() .NHibernateRepository();});Concurrency
Section titled “Concurrency”NHibernate natively supports multiple concurrency handling mechanisms. The easiest is probably adding a Version property of type int to the saga instance class and map it to the column with the same name