-
Notifications
You must be signed in to change notification settings - Fork 7
Using IoC DI Containers
REstate at its heart is powered by an IoC/DI container abstraction. This abstraction allows REstate to work with any DI container library for which an adapter is built. In fact, REstate has an embedded container that will be used if no other is provided.
REstate's assumption about dependencies is that they are always singleton. However, you can still register and resolve transient dependencies by registering a factory that builds your actual dependency when needed.
When you provide REstate a Container, it will register all of its default bindings within that Container. This allows you to individually resolve components of REstate from the container such as IAgent
or IStateEngine<,>
. Additionally, while REstate has its own semantics and interfaces for adding dependencies and components, if you rather, you can instead directly write to your Container for more flexibility.
Finally, any component or dependency registered in the Container can be constructor injected into any REstate component such as an Action or Precondition.
The adapter is available in a nuget package: REstate.IoC.Ninject
var yourNinjectKernel = new StanardKernel(); // Replace with your kernel.
var restateNinjectContainer = new NinjectComponentContainer(yourNinjectKernel);
At application startup, add the following line, building on the last snippet.
REstateHost.UseContainer(restateNinjectContainer);
Some scenarios don't lend well to using a shared instance of REstateHost
, such as if you have scoped kernels, or if you plan to inject REstateHost
as a dependency. This is also supported and is honestly even easier.
Whenever you need a host, just instantiate it with the REstate Container you made before.
var host = new REstateHost(restateNinjectContainer);