Skip to content

Commit

Permalink
Added support for dependency injection when creating actors
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutierrez committed Oct 16, 2023
1 parent 740a3da commit 81e4e1e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 9 deletions.
File renamed without changes.
19 changes: 16 additions & 3 deletions Nixie/ActorRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using Microsoft.Extensions.DependencyInjection;
using System.Collections.Concurrent;

namespace Nixie;
Expand All @@ -13,15 +14,19 @@ public sealed class ActorRepository<TActor, TRequest> : IActorRepositoryRunnable
{
private readonly ActorSystem actorSystem;

private readonly IServiceProvider? serviceProvider;

private readonly ConcurrentDictionary<string, Lazy<(ActorRunner<TActor, TRequest>, ActorRef<TActor, TRequest>)>> actors = new();

/// <summary>
/// Constructor
/// </summary>
/// <param name="actorSystem"></param>
public ActorRepository(ActorSystem actorSystem)
/// <param name="serviceProvider"></param>
public ActorRepository(ActorSystem actorSystem, IServiceProvider? serviceProvider)
{
this.actorSystem = actorSystem;
this.serviceProvider = serviceProvider;
}

/// <summary>
Expand Down Expand Up @@ -119,10 +124,18 @@ public bool IsProcessing()
for (int i = 0; i < args.Length; i++)
arguments[i + 1] = args[i];

actor = (TActor?)Activator.CreateInstance(typeof(TActor), arguments);
if (serviceProvider is not null)
actor = (TActor?)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TActor), arguments);
else
actor = (TActor?)Activator.CreateInstance(typeof(TActor), arguments);
}
else
actor = (TActor?)Activator.CreateInstance(typeof(TActor), actorContext);
{
if (serviceProvider is not null)
actor = (TActor?)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TActor), actorContext);
else
actor = (TActor?)Activator.CreateInstance(typeof(TActor), actorContext);
}

if (actor is null)
throw new NixieException("Invalid actor");
Expand Down
19 changes: 16 additions & 3 deletions Nixie/ActorRepositoryReply.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

using System.Collections.Concurrent;
using Microsoft.Extensions.DependencyInjection;

namespace Nixie;

Expand All @@ -13,15 +14,19 @@ public sealed class ActorRepository<TActor, TRequest, TResponse> : IActorReposit
{
private readonly ActorSystem actorSystem;

private readonly IServiceProvider? serviceProvider;

private readonly ConcurrentDictionary<string, Lazy<(ActorRunner<TActor, TRequest, TResponse>, ActorRef<TActor, TRequest, TResponse>)>> actors = new();

/// <summary>
/// Constructor
/// </summary>
/// <param name="actorSystem"></param>
public ActorRepository(ActorSystem actorSystem)
/// <param name="serviceProvider"></param>
public ActorRepository(ActorSystem actorSystem, IServiceProvider? serviceProvider)
{
this.actorSystem = actorSystem;
this.serviceProvider = serviceProvider;
}

/// <summary>
Expand Down Expand Up @@ -119,10 +124,18 @@ public bool IsProcessing()
for (int i = 0; i < args.Length; i++)
arguments[i + 1] = args[i];

actor = (TActor?)Activator.CreateInstance(typeof(TActor), arguments);
if (serviceProvider is not null)
actor = (TActor?)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TActor), arguments);
else
actor = (TActor?)Activator.CreateInstance(typeof(TActor), arguments);
}
else
actor = (TActor?)Activator.CreateInstance(typeof(TActor), actorContext);
{
if (serviceProvider is not null)
actor = (TActor?)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TActor), actorContext);
else
actor = (TActor?)Activator.CreateInstance(typeof(TActor), actorContext);
}

if (actor is null)
throw new NixieException("Invalid actor");
Expand Down
13 changes: 10 additions & 3 deletions Nixie/ActorSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public sealed class ActorSystem : IDisposable
{
private readonly ActorScheduler scheduler = new();

private readonly IServiceProvider? serviceProvider;

private readonly ConcurrentDictionary<Type, Lazy<IActorRepositoryRunnable>> repositories = new();

private readonly IActorRef<NobodyActor, object> nobody;
Expand All @@ -25,8 +27,13 @@ public sealed class ActorSystem : IDisposable
/// </summary>
public ActorScheduler Scheduler => scheduler;

public ActorSystem()
/// <summary>
/// Constructor
/// </summary>
public ActorSystem(IServiceProvider? serviceProvider = null)
{
this.serviceProvider = serviceProvider;

nobody = Spawn<NobodyActor, object>();
}

Expand Down Expand Up @@ -172,7 +179,7 @@ public ActorSystem()
private ActorRepository<TActor, TRequest, TResponse> CreateRepository<TActor, TRequest, TResponse>()
where TActor : IActor<TRequest, TResponse> where TRequest : class where TResponse : class
{
ActorRepository<TActor, TRequest, TResponse> repository = new(this);
ActorRepository<TActor, TRequest, TResponse> repository = new(this, serviceProvider);
return repository;
}

Expand All @@ -196,7 +203,7 @@ public ActorSystem()
private ActorRepository<TActor, TRequest> CreateRepository<TActor, TRequest>()
where TActor : IActor<TRequest> where TRequest : class
{
ActorRepository<TActor, TRequest> repository = new(this);
ActorRepository<TActor, TRequest> repository = new(this, serviceProvider);
return repository;
}

Expand Down
5 changes: 5 additions & 0 deletions Nixie/IActorReply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ namespace Nixie;
/// <typeparam name="TResponse"></typeparam>
public interface IActor<TRequest, TResponse> where TRequest : class where TResponse : class
{
/// <summary>
/// Passes a message to the actor and returns a response.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public Task<TResponse> Receive(TRequest message);
}
3 changes: 3 additions & 0 deletions Nixie/IGenericActorRef.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

namespace Nixie;

/// <summary>
/// Generic interface for actor references.
/// </summary>
public interface IGenericActorRef
{

Expand Down
4 changes: 4 additions & 0 deletions Nixie/Nixie.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
</ItemGroup>

</Project>

0 comments on commit 81e4e1e

Please sign in to comment.