Skip to content

NHibernate Configuration

alt NuGet

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; }
}

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
}
}

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 instance
container.RegisterSingleInstance<ISessionFactory>(...);
container.AddMassTransit(cfg =>
{
cfg.AddSagaStateMachine<OrderStateMachine, OrderState>()
.NHibernateRepository();
});

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