Skip to content

Instantly share code, notes, and snippets.

@Aaronontheweb
Last active March 4, 2024 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aaronontheweb/d7c46b8fa696c9770ccd378b23dec7a2 to your computer and use it in GitHub Desktop.
Save Aaronontheweb/d7c46b8fa696c9770ccd378b23dec7a2 to your computer and use it in GitHub Desktop.
GenericChildPerEntityParent Actor
// -----------------------------------------------------------------------
// <copyright file="GenericChildPerEntityParent.cs" company="Akka.NET Project">
// Copyright (C) 2015-2023 .NET Petabridge, LLC
// </copyright>
// -----------------------------------------------------------------------
using Akka.Actor;
using Akka.Cluster.Sharding;
namespace Sdkbin.NuGet.Actors.Utility;
/// <summary>
/// A generic "child per entity" parent actor.
/// </summary>
/// <remarks>
/// Intended for simplifying unit tests where we don't want to use Akka.Cluster.Sharding.
/// </remarks>
public sealed class GenericChildPerEntityParent : ReceiveActor
{
public static Props Props(IMessageExtractor extractor, Func<string, Props> propsFactory)
{
return Akka.Actor.Props.Create(() => new GenericChildPerEntityParent(extractor, propsFactory));
}
/*
* Re-use Akka.Cluster.Sharding's infrastructure here to keep things simple.
*/
private readonly IMessageExtractor _extractor;
private Func<string, Props> _propsFactory;
public GenericChildPerEntityParent(IMessageExtractor extractor, Func<string, Props> propsFactory)
{
_extractor = extractor;
_propsFactory = propsFactory;
ReceiveAny(o =>
{
var result = _extractor.EntityId(o);
if (result is null) return;
Context.Child(result).GetOrElse(() => Context.ActorOf(propsFactory(result), result)).Forward(_extractor.EntityMessage(o));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment