Walkthrough Ending
Lets tie up the loose ends so that we can run it. We need to orchestrate the startup of the actor system. Typically in Akkatecture this means instantiating:
- AggregateManagers
- AggregateSagaManagers
- DomainEventSubscribers
- Persistence Dependencies
The persistence dependencies is normally application specific. The RevenueRepository
in this walkthrough is an implementation detail and will depend on your persistence requirements.
Startup
We can make a method CreateActorSystem()
that instantiates all the actors accordingly:
//Walkthrough.Application.Program.cs//Create actor systemvar system = ActorSystem.Create("bank-system");//Create aggregate manager for accountsvar aggregateManager = system.ActorOf(Props.Create(() => new AccountManager()),"account-manager");//Create revenue repositoryvar revenueRepository = system.ActorOf(Props.Create(() => new RevenueRepository()),"revenue-repository");//Create subscriber for revenue repositorysystem.ActorOf(Props.Create(() => new RevenueSubscriber(revenueRepository)),"revenue-subscriber");//Create saga manager for money transfersystem.ActorOf(Props.Create(() =>new MoneyTransferSagaManager(() => new MoneyTransferSaga(aggregateManager))),"moneytransfer-saga");
Example Usage
In our console application's Main(...)
method:
//Walkthrough.Application.Program.cs//initialize actor systemCreateActorSystem();//create send receiver identifiersvar senderId = AccountId.New;var receiverId = AccountId.New;//create mock opening balancesvar senderOpeningBalance = new Money(509.23m);var receiverOpeningBalance = new Money(30.45m);//create commands for opening the sender and receiver accountsvar openSenderAccountCommand = new OpenNewAccountCommand(senderId, senderOpeningBalance);var openReceiverAccountCommand = new OpenNewAccountCommand(receiverId, receiverOpeningBalance);//send the command to be handled by the account aggregateAccountManager.Tell(openReceiverAccountCommand);AccountManager.Tell(openSenderAccountCommand);//create command to initiate money transfervar amountToSend = new Money(125.23m);var transaction = new Transaction(senderId, receiverId, amountToSend);var transferMoneyCommand = new TransferMoneyCommand(senderId,transaction);//send the command to initiate the money transferAccountManager.Tell(transferMoneyCommand);//fake 'wait' to let the saga process the chain of eventsawait Task.Delay(TimeSpan.FromSeconds(1));Console.WriteLine("Walkthrough operations complete.\n\n");Console.WriteLine("Press Enter to get the revenue:");Console.ReadLine();//get the revenue stored in the repositoryvar revenue = RevenueRepository.Ask<RevenueProjection>(new GetRevenueQuery(), TimeSpan.FromMilliseconds(500)).Result;//print the resultsConsole.WriteLine($"The Revenue is: {revenue.Revenue.Value}.");Console.WriteLine($"From: {revenue.Transactions} transaction(s).");
There we have our sample application working, congragulations for making it this far. Make sure to checkout the code and run the walkthrough. A full example can be found in the repository.
In Closing
Fantastic. You have finished the walkthrough. But don't stop there. Further your understanding by looking at some of the external resources that we have collected in video, and in article form.