A new message-typed-actor #6368
-
Recently I faced a bug that caused me to waste a lot of time. In my Receive-Actor, I had: Receive<Create> However, the Create message class was from the wrong namespace. In order to avoid such mistakes in the future, I decided to try and limit all messages in my actor to only those types of messages that inherit from my project's: MessageBase. public class TypedMessageActor : UntypedActor
{
protected override sealed void OnReceive(object message)
{
Validate(message);
var messageBase = (MessageBase)message;
Switch(messageBase);
}
private static void Validate(object message)
{
if (message is null)
{
throw new ApplicationException("message is null");
}
if (message is not MessageBase)
{
throw new ApplicationException("message is not MessageBase");
}
}
protected virtual void Switch(MessageBase messageBase)
{
switch (messageBase)
{
case Ping ping:
Handle(ping);
break;
case Pong pong:
Handle(pong);
break;
default:
throw new ApplicationException("Unhandled message");
break;
}
}
private void Handle(Ping _)
{
Sender.Tell(Pong.Instance);
}
private void Handle(Pong _)
{
Sender.Tell(Ping.Instance);
}
}
public class BodyActor : TypedMessageActor
{
protected override void Switch(MessageBase messageBase)
{
switch (messageBase)
{
case Create create: // if I use the Create from the wrong namespace - I get a compiler error
Handle(create);
break;
default:
base.Handle(messageBase);
break;
}
}
private void Handle(Create create)
{
Sender.Tell(Created.Instance);
}
} As you can see, this actor gives me all the functionality that I need. It is much simpler than the ReceiveActor with the added benefit of compile time message type. Would you like me to add this TypedMessageActor to the Akka library? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
We are actually just finishing removing an old |
Beta Was this translation helpful? Give feedback.
-
I am unable to see this message on github ( I guess it's a github bug). So
I have to respond here (via email reply):
On Fri, Jan 27, 2023 at 9:09 AM Gregorius Soedharmo < ***@***.***> wrote:
To be honest, this does not improve the API at all and it is breaking the
message processing convention.
You should never throw an exception on an unknown message, an exception
should only be thrown on an abnormal/erroneous state that breaks the actor,
you should have used the provided Unhandled() method instead.
I'll look into that. Thank you.
Have you tried to use the ReceiveActor class as a base?
Of course. But I could not get ReceiveActor to limit messages to only
inherit from my project's base-message. Compared to my message-typed-actor
- where I was able to get a compile time error if I used a bad message type
by mistake.
It is typed
It allows all types of messages. I need to block any message type except
for those that inherit from my project's base-message.
… and automatically handles unhandled messages for you.
Message ID: <akkadotnet/akka.net/repo-discussions/6368/comments/4797321@
github.com>
|
Beta Was this translation helpful? Give feedback.
We are actually just finishing removing an old
TypedActor
implementation from Akka.NET in v1.5 and in 2.0 we're going to introduce the ability to statically typeIActorRef
s based on the behavior of their upstream actors. So while I'm glad you found a pattern that is useful for you, I don't think we need this in the main repo for now given our future plans.