public async Task CanAppendToStreamAndReadFromStreamForwardAndGetJsonBack() { var streamStore = new InMemoryStreamStore(); var stremId = new StreamId("test"); var jsonIn = @"{ 'Hello': 'World' }"; var message = new NewStreamMessage(Guid.NewGuid(), "Test", jsonIn); await streamStore.AppendToStream(stremId, ExpectedVersion.Any, message); var readStreamPage = await streamStore.ReadStreamForwards(stremId, StreamVersion.Start, int.MaxValue); var jsonOut = await readStreamPage.Messages[0].GetJsonData(); Assert.Equal(1, readStreamPage.Messages.Length); Assert.Equal(jsonIn, jsonOut); }
public async Task ShowWithStream() { //setup a projection schema (one of many ways) var projector = new AsyncSqlProjector( Resolve.WhenEqualToHandlerMessageType(new PortfolioProjection()), new TransactionalSqlCommandExecutor( new ConnectionStringSettings( "projac", @"Data Source=(localdb)\ProjectsV12;Initial Catalog=ProjacUsage;Integrated Security=SSPI;", "System.Data.SqlClient"), IsolationLevel.ReadCommitted)); await projector.ProjectAsync(new object[] { new DropSchema(), new CreateSchema() }); //setup a memory eventstore var store = new InMemoryStreamStore(); //setup a sample stream (using some sample events) var portfolioId = Guid.NewGuid(); var events = new object[] { new PortfolioAdded { Id = portfolioId, Name = "My Portfolio" }, new PortfolioRenamed { Id = portfolioId, Name = "Your Portfolio" }, new PortfolioRemoved { Id = portfolioId } }; var stream = string.Format("portfolio-{0}", portfolioId.ToString("N")); await store.AppendToStream( stream, ExpectedVersion.Any, events .Select(@event => new NewStreamMessage( Guid.NewGuid(), @event.GetType().FullName, JsonConvert.SerializeObject(@event))) .ToArray()); //project the sample stream (until end of stream) var result = await store.ReadStreamForwards(stream, StreamVersion.Start, 1, true); foreach (var rawMessage in result.Messages) { var @event = JsonConvert.DeserializeObject( await rawMessage.GetJsonData(), Type.GetType(rawMessage.Type, true)); await projector.ProjectAsync(@event); } while (!result.IsEnd) { result = await store.ReadStreamForwards(stream, result.NextStreamVersion, 1, true); foreach (var rawMessage in result.Messages) { var @event = JsonConvert.DeserializeObject( await rawMessage.GetJsonData(), Type.GetType(rawMessage.Type, true)); await projector.ProjectAsync(@event); } } }