Skip to content

Integration With Other DI Containers

Max Vasilyev edited this page Jun 29, 2017 · 3 revisions

Integration With Other DI Containers

NSaga needs a Dependency Injection container. By default it comes with internal container implemented by TinyIoC. This is fine for basic use, but you might already have your own in your project. See Configuration section about how to configure internal container.

So currently there are 2 adapters for NSaga to work with SimpleInjector and Autofac.

SimpleInjector

To get this integration you'll have to get NSaga.SimpleInjector NuGet package.

Integration looks like this:

var container = new Container();

container.RegisterNSagaComponents()
    .UseSqlServer()
    .WithConnectionString(@"Data Source=.\SQLEXPRESS;integrated security=SSPI;Initial Catalog=NSaga");

container.Register<IEmailService, ConsoleEmailService>();
container.Register<ICustomerRepository, SimpleCustomerRepository>();

var mediator = container.GetInstance<ISagaMediator>();
var repository = container.GetInstance<ISagaRepository>();

This registers all Sagas in the container, also sets the storage to be in SQL Server. And registers Simple-Injector implementation of ISagaFactory.

Autofac

To get Autofac integration you need to get NSaga.Autofac NuGet package.

Integration looks like this:

var containerBuilder = new ContainerBuilder();

containerBuilder.RegisterNSagaComponents()
                .UseSqlServer()
                .WithConnectionString(@"Data Source=.\SQLEXPRESS;integrated security=SSPI;Initial Catalog=NSaga");

containerBuilder.RegisterType<ConsoleEmailService>().As<IEmailService>();
containerBuilder.RegisterType<SimpleCustomerRepository>().As<ICustomerRepository>();

var container = containerBuilder.Build();

var mediator = container.Resolve<ISagaMediator>();
var repository = container.Resolve<ISagaRepository>();

This registers all sagas with Autofac container, registers Autofac implementation of ISagaFactory and overrides storage to be SQL Server.

Structure Map

There is an adapter to work with structure map. Get it via NuGet: NSaga.StructureMap

var container = new Container();
container.RegisterNSagaComponents();

var mediator = container.GetInstance<ISagaMediator>();

var correlationId = Guid.NewGuid();
var initMessage = new PersonalDetailsVerification(correlationId);

mediator.Consume(initMessage);

Other Containers

Currently there are no other container supported. But it is relatively easy to make one, please let us know if you would like to use NSaga with your container of choice - we'll see what we can do.