-
Notifications
You must be signed in to change notification settings - Fork 19
Configuration
Configuration for NSaga is basically configuring Dependency Injection container to insert correct components into SagaMediator
.
Out of the box NSaga comes with internal DI container - TinyIoC. But the configuration of components should happen through SagaMediatorBuilder
. If you already have a DI container, you can skip this section and read Integration With Other DI Containers.
SagaMediatorBuilder
is a wrapper around TinyIoC container with some specific API for registrations of NSaga components.
var builder = Wireup.UseInternalContainer();
var mediator = builder.ResolveMediator();
It scans currently loaded assemblies for saga classes. Usually this is enough for most needs, but you might want to scan only specific assemblies. You can repace the whole list or add only specific ones:
class ReplaceAssemblies
{
public ReplaceAssemblies()
{
var builder = Wireup.UseInternalContainer()
.ReplaceAssembliesToScan(new Assembly[] { typeof(ReplaceAssemblies).Assembly })
.AddAssembliesToScan(new Assembly[] {typeof(VerySimpleSaga).Assembly});
}
}
The default configuration gives you only in-memory persistence: InMemorySagaRepository
which uses static concurrent dictionaries. This will be erased when you restart your application. You can replace implementation of ISagaRepository
with your own type:
var builder = Wireup.UseInternalContainer()
.UseRepository<DummySagaRepository>();
To replace the way messages are serialised you need to implement IMessageSerialiser
and replace it in the builder:
var builder = Wireup.UseInternalContainer()
.UseMessageSerialiser<DummyMessageSerialiser>();
You can replace the way sagas are resolved, though it will be strange to do it when using internal container, you can do it. Sagas are resolved by ISagaFactory
and to replace the default you need to register type in builder:
var builder = Wireup.UseInternalContainer()
.UseSagaFactory<DummySagaFactory>();
You can add more pipeline hooks by implementing IPipelineHook
. More about this in Pipeline Hooks. Add this into builder like this:
var builder = Wireup.UseInternalContainer()
.AddPiplineHook<DummyPipelineHook>();
If you are going to use sagas in more than one place, you can store your instance of SagaMediatorBuilder
in global static reference: SagaMediatorBuilder.Current
:
SagaMediatorBuilder.Current
.UseSqlServer()
.WithConnectionStringName("NSagaDatabase");
var sagaMediator = SagaMediatorBuilder.Current.ResolveMediator();
or assign to the static reference when the builder is configured:
var builder = Wireup.UseInternalContainer()
.UseRepository<DummySagaRepository>()
.UseMessageSerialiser<DummyMessageSerialiser>()
.UseSagaFactory<DummySagaFactory>();
SagaMediatorBuilder.Current = builder;
var sagaMediator = SagaMediatorBuilder.Current.ResolveMediator();
As you might have noticed there is a limited number of interfaces available to implment and replace:
-
IMessageSerialiser
. By default providedJsonNetSerialiser
. Used to serialise and deserialise messages andSagaData
; -
ISagaRepository
. By default providedInMemorySagaRepository
. Used to store saga states and saga headers. You can also useSqlSagaRepository
-
ISagaFactory
. By default providedTinyIocSagaFactory
. This is an abstraction around DI container to resolve saga instances, but this does not deal with saga state. -
IEnumerable<IPipelineHook>
. By default providedMetadataPipelineHook
. This is an extension point to do any metadata modificaitons to sagas. See Pipeline Hooks for more information.