23. How do we know if
something has
State?
If a function is invoked
with the same
arguments at
two different points in time
and returns different
values...
31. Let’s write an
atomic transfer method
public class Account {
...
public synchronized void
transfer(Account to, int amount) {
this.withdraw(amount);
to.deposit(amount);
}
...
}
This will work right?
32. Let’s transfer funds
Account alice = ...
Account bob = ...
alice.transferTo(bob, 10.0D);
bob.transferTo(alice, 3.0D);
33. Let’s transfer funds
Account alice = ...
Account bob = ...
alice.transferTo(bob, 10.0D);
bob.transferTo(alice, 3.0D);
35. Weneed to enforce lock
ordering
>How?
>Java or C# won’t help us
>Need to use code convention (names
etc.)
>Requires knowledge about the internal
state and implementation of Account
>…runscounter to the principles
of encapsulation in OOP
75. This Is AnAkka.NET Actor
public class FooActor : UntypedActor
{
public FooActor()
{
// message handling code goes here...
}
}
76. These Are Messages
// just POCOs
public class
public class
Hello {}
NewOrder {
(string name)public NewOrder
{
Username
UserId =
= name;
Guid.NewGuid ();
}
Guid UserId { get; private set;}public
public string Username {get; private set;}
}
82. //immutable message for withdraw:
public class TransferRequest
{
public string TransactionID;
public decimal Amount;
public string From;
public string To;
}
//the account actor
public class TransferManager : TypedActor, IHandle<Withdraw>
{
public void Handle(TransferRequest e)
{
...
}
}
84. //immutable message for withdraw:
public class TransferRequest
{
public string TransactionID;
public decimal Amount;
public string From;
public string To;
}
//the account actor
public class TransferManager : TypedActor, IHandle<Withdraw>
{
public void Handle(TransferRequest e)
{
var fromAddress = "akka://my-sys/accounts/" + e.From;
Context.ActorSelection(fromAddress).Tell(new Withdraw(e.Amount));
}
}
90. //immutable message for withdraw:
public class TransferRequest
{
public string TransactionID;
public decimal Amount;
public string From;
public string To;
}
//the account actor
public class TransferManager : TypedActor, IHandle<Withdraw>
{
public void Handle(TransferRequest e)
{
Context.ActorSelection(transfer.From).Tell(new Withdraw(e.Amount));
Context.ActorSelection(transfer.To).Tell(new Deposit(e.Amount));
Sender.Tell(new TransferSuccess(e.TransactionID));
}
}