# Simple DynamoDB Persistence Usage This sample shows a client/server scenario using saga persistence. ## Prerequisites This sample uses a [DynamoDB local instance](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html) by default. See the [AWS guidance on deploying DynamoDB local](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html#docker). Alternatively with Docker installed locally, execute the following command in the solution directory: ```bash docker-compose up -d ``` the data is only kept in memory and will be gone when the container is removed. ## Projects ### Client * Sends the `StartOrder` message to `Server`. * Receives and handles the `OrderCompleted` event. ### Server projects * Receive the `StartOrder` message and initiate an `OrderSaga`. * `OrderSaga` requests a timeout with an instance of `CompleteOrder` with the saga data. * `OrderSaga` publishes an `OrderCompleted` event when the `CompleteOrder` timeout fires. ### SharedMessages Contains the shared message contracts used by all endpoints. ## Persistence config Configure the endpoint to use DynamoDB Persistence. ```cs var amazonDynamoDbClient = new AmazonDynamoDBClient( new BasicAWSCredentials("localdb", "localdb"), new AmazonDynamoDBConfig { ServiceURL = "http://localhost:8000" }); var endpointConfiguration = new EndpointConfiguration("Samples.DynamoDB.Simple.Server"); var persistence = endpointConfiguration.UsePersistence(); persistence.DynamoClient(amazonDynamoDbClient); persistence.UseSharedTable(new TableConfiguration { TableName = "Samples.DynamoDB.Simple" }); ``` ## Order saga data ```cs public class OrderSagaData : ContainSagaData { public Guid OrderId { get; set; } public string OrderDescription { get; set; } } ``` ## Order saga ```cs public class OrderSaga(ILogger logger) : Saga, IAmStartedByMessages, IHandleTimeouts { protected override void ConfigureHowToFindSaga(SagaPropertyMapper mapper) { mapper.MapSaga(saga => saga.OrderId).ToMessage(msg => msg.OrderId); } public Task Handle(StartOrder message, IMessageHandlerContext context) { var orderDescription = $"The saga for order {message.OrderId}"; Data.OrderDescription = orderDescription; logger.LogInformation("Received StartOrder message {OrderId}. Starting Saga", message.OrderId); var shipOrder = new ShipOrder { OrderId = message.OrderId }; logger.LogInformation("Order will complete in 5 seconds"); var timeoutData = new CompleteOrder { OrderDescription = orderDescription, }; return Task.WhenAll( context.SendLocal(shipOrder), RequestTimeout(context, TimeSpan.FromSeconds(5), timeoutData) ); } public Task Timeout(CompleteOrder state, IMessageHandlerContext context) { logger.LogInformation("Saga with OrderId {OrderId} completed", Data.OrderId); MarkAsComplete(); var orderCompleted = new OrderCompleted { OrderId = Data.OrderId }; return context.Publish(orderCompleted); } } ```