我的演员系统是在控制台应用程序上初始化的。我配置了一个名为 BankAccrountActor 的参与者,它负责处理银行账户交易,如账户的贷方/借方/账户余额。在贷记/借记命令中,参与者将从帐户和 Sender.Tell() 中贷记/借记剩余余额。从我的 program.cs 我打电话给 BankAccountActor 并要求借记并获得结果余额。
对我来说问题来自 BankAccountActor 它调用 Sender.Tell() 余额。这里 sender 是 IActorRef 类型,结果是程序类,程序不是演员。怎么可能?
namespace BankAccount
{
class Program
{
static void Main(string[] args)
{
ActorSystem actorSystem = ActorSystem.Create("BankAccountSystem");
Props bankAccountAccProps = Props.Create<BankAccountActor>();
IActorRef bankAccActor = actorSystem.ActorOf(bankAccountAccProps, "abc");
var remainBalance = bankAccActor.Ask<decimal>(new DebitAccMessage(100)).Result;
}
}
internal class BankAccountActor : ReceiveActor
{
private decimal _accBalance = 100;
public BankAccountActor()
{
Receive<DebitAccMessage>(message => Debit(message));
Receive<CreditAccMessage>(message => Credit(message));
}
public void Debit(DebitAccMessage debitMessage)
{
_accBalance += debitMessage.Amount;
Sender.Tell(_accBalance);
}
public void Credit(CreditAccMessage debitMessage)
{
_accBalance -= debitMessage.Amount;
Sender.Tell(_accBalance);
}
}
internal class CreditAccMessage
{
public CreditAccMessage(decimal amount)
{
Amount = amount;
}
public decimal Amount { get; private set; }
}
internal class DebitAccMessage
{
public DebitAccMessage(decimal amount)
{
Amount = amount;
}
public decimal Amount { get; private set; }
}
}