Git Product home page Git Product logo

akka.di.simpleinjector's Introduction

Akka.DI.SimpleInjector

Actor Producer Extension backed by the Simple Injector Dependency Injection Container for the Akka.NET framework.

What is it?

Akka.DI.SimpleInjector is an ActorSystem extension for the Akka.NET framework that provides an alternative to the basic capabilities of Props when you have Actors with multiple dependencies.

If Simple Injector is your IoC container of choice and your actors have dependencies that make using the factory method provided by Props prohibitive and code maintenance is an important concern then this is the extension for you.

How to you use it?

The best way to understand how to use it is by example. If you are already considering this extension then we will assume that you know how how to use the Simple Injector container. This example is demonstrating a system using ConsistentHashing routing along with this extension.

Start by creating your container and registering your actors and dependencies.

// Setup Simple Injector
var container = new Container();
container.Register<IWorkerService, WorkerService>();
container.Register<ITypedWorker, TypedWorker>();

Next you have to create your ActorSystem and inject that system reference along with the container reference into a new instance of the SimpleInjectorDependencyResolver.

// Create the ActorSystem
using (var system = ActorSystem.Create("MySystem"))
{
    // Create the dependency resolver
    IDependencyResolver resolver = new SimpleInjectorDependencyResolver(container, system);

    // we'll fill in the rest in the following steps
}

To register the actors with the system use method Akka.Actor.Props Create<TActor>() of the IDependencyResolver interface implemented by the SimpleInjectorDependencyResolver.

// Register the actors with the system
system.ActorOf(resolver.Create<TypedWorker>(), "Worker1");
system.ActorOf(resolver.Create<TypedWorker>(), "Worker2");

Finally create your router, message and send the message to the router.

// Create the router
IActorRef router = system.ActorOf(Props.Empty.WithRouter(new ConsistentHashingGroup(config)));

// Create the message to send
TypedActorMessage message = new TypedActorMessage
{
   Id = 1,
   Name = Guid.NewGuid().ToString()
};

// Send the message to the router
router.Tell(message);

The resulting code should look similar to the the following:

// Setup Simple Injector
var container = new Container();
container.Register<IWorkerService, WorkerService>();
container.Register<ITypedWorker, TypedWorker>();

// Create the ActorSystem
using (var system = ActorSystem.Create("MySystem"))
{
    // Create the dependency resolver
    IDependencyResolver resolver = new SimpleInjectorDependencyResolver(container, system);

    // Register the actors with the system
    system.ActorOf(resolver.Create<TypedWorker>(), "Worker1");
    system.ActorOf(resolver.Create<TypedWorker>(), "Worker2");

    // Create the router
    IActorRef router = system.ActorOf(Props.Empty.WithRouter(new ConsistentHashingGroup(config)));

    // Create the message to send
    TypedActorMessage message = new TypedActorMessage
    {
       Id = 1,
       Name = Guid.NewGuid().ToString()
    };

    // Send the message to the router
    router.Tell(message);
}

akka.di.simpleinjector's People

Contributors

aaronontheweb avatar alexvaluyskiy avatar carlostorrecillas avatar danthar avatar heynickc avatar horusiath avatar marcpiechura avatar sean-gilliam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

akka.di.simpleinjector's Issues

Any ideas how to avoid container.Verify(); problems?

Hi there.

Just wanted to ask if someone has a clue on how to avoid errors on container.Verify().

When you verify the container it tries to create all registrations. Which isn't really good for actor classes as one would know. Not that it actually is a problem, but it throws annoying error messages like "no active Context or ActorSystem or whatnot".

Build script and CI integration

The proper build infrastructure needs to be added to be able to integrate with our CI environment.
As a basis the build infrastructure from the logging repos can be used.

Only difference is that in this repo tests also needs to be included and run.

Container lifestyle issues

In the implementation code "AsyncScopedLifestyle" is used but in the docs example it is not

// Setup Simple Injector
var container = new Container();
container.Register<IWorkerService, WorkerService>();

//this code should be added like in unit tests (set lifestyle for scoped registrations)
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

//and maybe even (default all registrations to scoped)
container.Options.DefaultLifestyle = new AsyncScopedLifestyle();

since the alternative would be to set .Register<...>(Lifestyle.Scoped) for all registrations. If this is not set they will default to transient.

Another point is to have at least one test to check .Dispose() is called. A typical scenario is to have a Entity framework DbContext registered and it should be disposed at the end of the scope.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.