0

I'm trying to pass on certain information about a hierarchy of actors so that's available when they receive messages. For example, I have one actor chain per user and I would like to make available the user information (name) across all children of that actor.

At the moment I'm trying to access the user actor from any children in the hierarchy to get the name but a) I don't know if that's a good practice and b) A simply don't know if I can achieve that when I have more than one level in the hierarchy and the user name is obviously dynamic.

So I've tried this (assume I want to access the parent's parent)

var name = Context.ActorSelection("../..").Path.Name;

That doesn't return anything useful and doesn't seem to go up two levels in the hierarchy.

The other option I thought was creating the actor hierarchy and construct all the nodes below with Props and passing in the user name and again, I don't know if that's a good practice either/the right thing to do. So for example:

public class MyActor: TypedActor
{
    public MyActor(string id)
    {
        _id = id;
        Context.ActorOf(Props.Create<ChildActor>(_id), "childname");
    }
}

And so on...

Carlos Torrecillas
  • 4,965
  • 7
  • 38
  • 69

1 Answers1

3

I don't know what kind of problem you are going to solve, but I could suggest to not couple userId in actor constructor (props)

in root actor you could store a list (dict) with user actors reference and users id, then in child actor using <PRESTART> action ask for user ID.

protected override void PreStart()
        {
            Context.Parent.Tell(new GetUserIdMessage());
            base.PreStart();
        }

then add Receive<UserIdMessage> and store this in a field inside an actor This will allow write code without coupling and this extra few messages shouldn't have impact on system performance

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
profesor79
  • 9,213
  • 3
  • 31
  • 52