Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created August 22, 2019 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/0611ee53cf8b5ed83cbe3f2c0ec6ddf8 to your computer and use it in GitHub Desktop.
Save dcomartin/0611ee53cf8b5ed83cbe3f2c0ec6ddf8 to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SqlStreamStore.Streams;
namespace SqlStreamStore.Demo
{
public class BalanceProjection
{
public Balance Balance { get; private set; } = new Balance(0, DateTime.UtcNow);
public BalanceProjection(IStreamStore streamStore, StreamId streamId)
{
streamStore.SubscribeToStream(streamId, null, StreamMessageReceived);
}
private async Task StreamMessageReceived(IStreamSubscription subscription, StreamMessage streamMessage, CancellationToken cancellationToken)
{
switch (streamMessage.Type)
{
case "Deposited":
var depositedJson = await streamMessage.GetJsonData(cancellationToken);
var deposited = JsonConvert.DeserializeObject<Deposited>(depositedJson);
Balance = Balance.Add(deposited.Amount);
break;
case "Withdrawn":
var withdrawnJson = await streamMessage.GetJsonData(cancellationToken);
var withdrawn = JsonConvert.DeserializeObject<Withdrawn>(withdrawnJson);
Balance = Balance.Subtract(withdrawn.Amount);
break;
}
}
}
public struct Balance
{
public Balance(decimal amount, DateTime asOf)
{
Amount = amount;
AsOf = asOf;
}
public decimal Amount { get; }
public DateTime AsOf { get; }
public Balance Add(decimal value)
{
return new Balance(Amount + value, DateTime.UtcNow);
}
public Balance Subtract(decimal value)
{
return new Balance(Amount - value, DateTime.UtcNow);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment