Skip to content

heynickc/Akka.DI.CastleWindsor

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Akka.DI.CastleWindsor

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

What is it?

Akka.DI.CastleWindsor 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 CastleWindsor 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 CastleWindsor 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 CastleWindsor
IWindsorContainer container = new WindsorContainer();
container.Register(Component.For<IWorkerService>().ImplementedBy<WorkerService>());
container.Register(Component.For<TypedWorker>().Named("TypedWorker").LifestyleTransient());

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

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

// 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 CastleWindsor
IWindsorContainer container = new WindsorContainer();
container.Register(Component.For<IWorkerService>().ImplementedBy<WorkerService>());
container.Register(Component.For<TypedWorker>().Named("TypedWorker").LifestyleTransient());

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

About

Akka.NET integration plugin for Castle.Windsor dependency injection library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 41.1%
  • PowerShell 30.6%
  • F# 28.1%
  • Batchfile 0.2%