Akka.NET integration plugin for AutoFac dependency injection library
Перейти к файлу
Aaron Stannard 516ff4bc75
Updates for 1.4.1 release (#53) (#54)
Co-authored-by: Gregorius Soedharmo <arkatufus@yahoo.com>
2020-03-11 14:57:16 -05:00
build-system Update build system 2019-10-07 18:36:46 +02:00
docs upgraded to use Petabridge.Templates dotnet new build system (#19) 2018-11-01 12:25:03 -05:00
src Updates for 1.4.1 release (#53) (#54) 2020-03-11 14:57:16 -05:00
.gitattributes Add `UseAutofac` extension method to configure Autofac in `ActorSystem`s 2018-10-14 04:00:22 -03:00
.gitignore Update build system 2019-10-07 18:36:46 +02:00
Akka.DI.AutoFac.sln upgraded to use Petabridge.Templates dotnet new build system (#19) 2018-11-01 12:25:03 -05:00
LICENSE upgraded to use Petabridge.Templates dotnet new build system (#19) 2018-11-01 12:25:03 -05:00
RELEASE_NOTES.md Updates for 1.4.1 release (#53) (#54) 2020-03-11 14:57:16 -05:00
Readme.md upgraded to use Petabridge.Templates dotnet new build system (#19) 2018-11-01 12:25:03 -05:00
build.cmd Added support for NetStandard 2017-08-06 19:51:31 +03:00
build.fsx Upgrade build system (#43) 2020-03-02 21:48:21 -06:00
build.ps1 Upgrade build system (#43) 2020-03-02 21:48:21 -06:00
build.sh Upgrade build system (#43) 2020-03-02 21:48:21 -06:00
serve-docs.cmd upgraded to use Petabridge.Templates dotnet new build system (#19) 2018-11-01 12:25:03 -05:00
serve-docs.ps1 upgraded to use Petabridge.Templates dotnet new build system (#19) 2018-11-01 12:25:03 -05:00

Readme.md

Akka.DI.Autofac

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

What is it?

Akka.DI.Autofac 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 Autofac 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 Autofac container. This example is demonstrating a system using ConsistentHashing routing along with this extension.

Start by creating your builder, registering your actors and dependencies, and build your container.

// Setup Autofac
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<WorkerService>().As<IWorkerService>();
builder.RegisterType<TypedWorker>();
IContainer container = builder.Build();

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

// Create the ActorSystem
using (var system = ActorSystem.Create("MySystem"))
{
    // Create the dependency resolver
    IDependencyResolver resolver = new AutoFacDependencyResolver(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 AutoFacDependencyResolver.

// 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 Autofac
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<WorkerService>().As<IWorkerService>();
builder.RegisterType<TypedWorker>();
IContainer container = builder.Build();

// Create the ActorSystem
using (var system = ActorSystem.Create("MySystem"))
{
    // Create the dependency resolver
    IDependencyResolver resolver = new AutoFacDependencyResolver(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);
}