简体   繁体   中英

Inheritance with Akka.Net actors

I'm wondering, how to implement inheritance with Akka.Net. I want base actor to handle base messages and child actor to handle child messages.

For example, I have following hierarchies:

public class BaseMessage
{
    public string Data { get; set; }
}

public class ChildMessage : BaseMessage
{
    public string ChildData { get ;set; }
}

public abstract class BaseActor : ReceiveActor
{
    private string baseData;

    public BaseActor()
    {
        Receive<BaseMessage>(m => {
            baseData = m.Data;
        });

        // be aware that adding ReceiveAny handler in base class means that you wont be able to add any handlers in descendant actors
        // just override Unhandled method
    }
}

public class MyActor : BaseActor 
{
    private string myData;    

    public MyActor()
    {
        Receive<ChildMessage>(m => {
        myData = m.ChildData;

        // obviously BaseActor won't receive BaseMessage
        // so I should somehow send BaseMessage to it

        // option 1, not sure will it work
        BaseMessage baseMessage = m as BaseMessage;
        Self.Tell(baseMessage);

        // option 2, definitely should work
        BaseMessage baseMessage = new BaseMessage { Data = m.Data };
        Self.Tell(baseMessage);
        });
    }
}

Question is: will option 1 to send message to parent actor work?

Unfortunatelly, it won't work in this way, because parent actor will intercept ChildMessage sent to child actor. So, my solution is below

public class BaseMessage
{
    public string Data { get; set; }
}

public class ChildMessage : BaseMessage
{
    public string ChildData { get ;set; }
}

public abstract class BaseActor : ReceiveActor
{
    private string baseData;

    public BaseActor()
    {
        Receive<BaseMessage>(m => {
            ProcessMessage(m);
        });

        // be aware that adding ReceiveAny handler in base class means that you wont be able to add any handlers in descendant actors
        // just override Unhandled method instead
    }

    protected virtual void ProcessMessage(BaseMessage m)
    {
        baseData = m.Data;
    }
}


public class MyActor: BaseActor
{
    private string myData;    

    public MyActor()
    {
        // no interceptor for ChildMessage here, because parent class has interceptor for BaseMessage and it will handle ChildMessage too
    }

    protected override void ProcessMessage(BaseMessage m)
    {
        base.ProcessMessage(m);

        // not qute OOP way, but it works
        var childMessage = m as ChildMessage;
        if(childMessage != null)
        {
            myData = childMessage.ChildData;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM