Skip to content

Commit

Permalink
introduced non-breaking Akka.DependencyInjection API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed May 25, 2021
1 parent a5bfb93 commit 7815c96
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,47 @@ public static DependencyResolver For(ActorSystem actorSystem)
/// <returns>A new <see cref="Akka.Actor.Props"/> instance which uses DI internally.</returns>
public Props Props<T>(params object[] args) where T : ActorBase
{
return Props(typeof(T), args);
return Resolver.Props<T>(args);
}


/// <summary>
/// Used to dynamically instantiate an actor where some of the constructor arguments are populated via dependency injection
/// and others are not.
/// </summary>
/// <remarks>
/// YOU ARE RESPONSIBLE FOR MANAGING THE LIFECYCLE OF YOUR OWN DEPENDENCIES. AKKA.NET WILL NOT ATTEMPT TO DO IT FOR YOU.
/// </remarks>
/// <typeparam name="T">The type of actor to instantiate.</typeparam>
/// <returns>A new <see cref="Akka.Actor.Props"/> instance which uses DI internally.</returns>
public Props Props<T>() where T : ActorBase
{
return Props(typeof(T));
return Resolver.Props<T>();
}


/// <summary>
/// Used to dynamically instantiate an actor where some of the constructor arguments are populated via dependency injection
/// and others are not.
/// </summary>
/// <remarks>
/// YOU ARE RESPONSIBLE FOR MANAGING THE LIFECYCLE OF YOUR OWN DEPENDENCIES. AKKA.NET WILL NOT ATTEMPT TO DO IT FOR YOU.
/// </remarks>
/// <param name="type">The type of actor to instantiate.</param>
/// <returns>A new <see cref="Akka.Actor.Props"/> instance which uses DI internally.</returns>
public Props Props(Type type)
{
return Resolver.Props(type);
}


/// <summary>
/// Used to dynamically instantiate an actor where some of the constructor arguments are populated via dependency injection
/// and others are not.
/// </summary>
/// <remarks>
/// YOU ARE RESPONSIBLE FOR MANAGING THE LIFECYCLE OF YOUR OWN DEPENDENCIES. AKKA.NET WILL NOT ATTEMPT TO DO IT FOR YOU.
/// </remarks>
/// <param name="type">The type of actor to instantiate.</param>
/// <param name="args">Optional. Any constructor arguments that will be passed into the actor's constructor directly without being resolved by DI first.</param>
/// <returns>A new <see cref="Akka.Actor.Props"/> instance which uses DI internally.</returns>
public Props Props(Type type, params object[] args)
{
return Resolver.Props(type, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@

namespace Akka.DependencyInjection
{
/// <summary>
/// Used to help bootstrap an <see cref="ActorSystem"/> with dependency injection (DI)
/// support via a <see cref="IServiceProvider"/> reference.
///
/// The <see cref="IServiceProvider"/> will be used to access previously registered services
/// in the creation of actors and other pieces of infrastructure inside Akka.NET.
///
/// The constructor is internal. Please use <see cref="Create"/> to create a new instance.
/// </summary>
[Obsolete("Used DependencyResolverSetup instead.")]
public class ServiceProviderSetup : Setup
{
internal ServiceProviderSetup(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}

public IServiceProvider ServiceProvider { get; }

public static ServiceProviderSetup Create(IServiceProvider provider)
{
if (provider == null)
throw new ArgumentNullException(nameof(provider));

return new ServiceProviderSetup(provider);
}
}

/// <summary>
/// Used to help bootstrap an <see cref="ActorSystem"/> with dependency injection (DI)
/// support via a <see cref="IDependencyResolver"/> reference.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,52 @@

namespace Akka.DependencyInjection
{
/// <summary>
/// Interface abstraction for working with DI providers
/// in Akka.NET without being bound to any specific implementation.
/// </summary>
/// <remarks>
/// See <see cref="ServiceProviderDependencyResolver"/> for a reference implementation.
/// </remarks>
public interface IDependencyResolver
{
IResolverScope CreateScope();
object GetService<T>();
object GetService(Type type);

/// <summary>
/// Used to dynamically instantiate an actor where some of the constructor arguments are populated via dependency injection
/// and others are not.
/// </summary>
/// <remarks>
/// YOU ARE RESPONSIBLE FOR MANAGING THE LIFECYCLE OF YOUR OWN DEPENDENCIES. AKKA.NET WILL NOT ATTEMPT TO DO IT FOR YOU.
/// </remarks>
/// <param name="type">The type of actor to instantiate.</param>
/// <param name="args">Optional. Any constructor arguments that will be passed into the actor's constructor directly without being resolved by DI first.</param>
/// <returns>A new <see cref="Akka.Actor.Props"/> instance which uses DI internally.</returns>
Props Props(Type type, params object[] args);

/// <summary>
/// Used to dynamically instantiate an actor where some of the constructor arguments are populated via dependency injection
/// and others are not.
/// </summary>
/// <remarks>
/// YOU ARE RESPONSIBLE FOR MANAGING THE LIFECYCLE OF YOUR OWN DEPENDENCIES. AKKA.NET WILL NOT ATTEMPT TO DO IT FOR YOU.
/// </remarks>
/// <param name="type">The type of actor to instantiate.</param>
/// <returns>A new <see cref="Akka.Actor.Props"/> instance which uses DI internally.</returns>
Props Props(Type type);

/// <summary>
/// Used to dynamically instantiate an actor where some of the constructor arguments are populated via dependency injection
/// and others are not.
/// </summary>
/// <remarks>
/// YOU ARE RESPONSIBLE FOR MANAGING THE LIFECYCLE OF YOUR OWN DEPENDENCIES. AKKA.NET WILL NOT ATTEMPT TO DO IT FOR YOU.
/// </remarks>
/// <typeparam name="T">The type of actor to instantiate.</typeparam>
/// <param name="args">Optional. Any constructor arguments that will be passed into the actor's constructor directly without being resolved by DI first.</param>
/// <returns>A new <see cref="Akka.Actor.Props"/> instance which uses DI internally.</returns>
Props Props<T>(params object[] args) where T : ActorBase;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ namespace Akka.DependencyInjection
/// Provides users with immediate access to the <see cref="IServiceProvider"/> bound to
/// this <see cref="ActorSystem"/>, if any.
/// </summary>
/// <remarks>
/// [OBSOLETE] Switch to the <see cref="DependencyResolver"/> instead.
/// </remarks>
[Obsolete("Replaced by the Akka.DependencyInjection.DependencyResolver in Akka.NET v1.4.20. Please switch to that.")]
public sealed class ServiceProvider : IExtension
{
public ServiceProvider(IServiceProvider provider)
Expand Down Expand Up @@ -66,6 +70,7 @@ public Props Props<T>(params object[] args) where T : ActorBase
/// <summary>
/// INTERNAL API
/// </summary>
[Obsolete("Use the DependencyResolverExtensions instead.")]
public sealed class ServiceProviderExtension : ExtensionIdProvider<ServiceProvider>
{
public override ServiceProvider CreateExtension(ExtendedActorSystem system)
Expand All @@ -88,17 +93,16 @@ public override ServiceProvider CreateExtension(ExtendedActorSystem system)
///
/// Used to create actors via the <see cref="ActivatorUtilities"/>.
/// </summary>
/// <typeparam name="TActor">the actor type</typeparam>
internal sealed class ServiceProviderActorProducer<TActor> : IIndirectActorProducer where TActor:ActorBase
internal class ServiceProviderActorProducer : IIndirectActorProducer
{
private readonly IServiceProvider _provider;
private readonly object[] _args;

public ServiceProviderActorProducer(IServiceProvider provider, object[] args)
public ServiceProviderActorProducer(IServiceProvider provider, Type actorType, object[] args)
{
_provider = provider;
_args = args;
ActorType = typeof(TActor);
ActorType = actorType;
}

public ActorBase Produce()
Expand All @@ -113,4 +117,19 @@ public void Release(ActorBase actor)
// no-op
}
}

/// <summary>
/// INTERNAL API
///
/// Used to create actors via the <see cref="ActivatorUtilities"/>.
/// </summary>
/// <typeparam name="TActor">the actor type</typeparam>
internal class ServiceProviderActorProducer<TActor> : ServiceProviderActorProducer where TActor:ActorBase
{

public ServiceProviderActorProducer(IServiceProvider provider, object[] args)
: base(provider, typeof(TActor), args)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace Akka.DependencyInjection
{
/// <summary>
/// INTERNAL API.
///
/// <see cref="IDependencyResolver"/> implementation backed by <see cref="IServiceProvider"/>
/// </summary>
public class ServiceProviderDependencyResolver : IDependencyResolver
{
public IServiceProvider ServiceProvider { get; }
Expand All @@ -37,12 +42,17 @@ public object GetService(Type type)

public Props Props(Type type, params object[] args)
{
return new ServiceProviderProps(ServiceProvider, type, args);
return Akka.Actor.Props.CreateBy(new ServiceProviderActorProducer(ServiceProvider, type, args));
}

public Props Props(Type type)
{
return new ServiceProviderProps(ServiceProvider, type);
return Akka.Actor.Props.CreateBy(new ServiceProviderActorProducer(ServiceProvider, type, Array.Empty<object>()));
}

public Props Props<T>(params object[] args) where T : ActorBase
{
return Akka.Actor.Props.CreateBy(new ServiceProviderActorProducer<T>(ServiceProvider, Array.Empty<object>()));
}
}

Expand Down

This file was deleted.

0 comments on commit 7815c96

Please sign in to comment.