Esempio n. 1
0
        private void ConfigureProjections(IEventStoreConnection connection)
        {
            var projector = new SqlProjector(
                Resolve.WhenEqualToHandlerMessageType(new CustomerProjection()),
                new TransactionalSqlCommandExecutor(
                    new ConnectionStringSettings(
                        "projac",
                        @"Data Source=.\SQLEXPRESS;Initial Catalog=Projections;Integrated Security=SSPI;",
                        "System.Data.SqlClient"),
                    IsolationLevel.ReadCommitted));

            projector.Project(new object[] { new DropSchema(), new CreateSchema() });

            var position = Position.Start;

            connection.SubscribeToAllFrom(position, false, (subscription, @event) =>
            {
                object data = null;
                try
                {
                    data = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(@event.Event.Data),
                                                         Type.GetType(@event.Event.EventType, false));
                }
                catch
                {
                    // ignored
                }
                if (data != null)
                {
                    projector.Project(data);
                }
            });
        }
Esempio n. 2
0
        public async Task ShowWithCatchupSubscription()
        {
            //setup a projection schema (one of many ways)
            var projector = new SqlProjector(
                Resolve.WhenEqualToHandlerMessageType(new PortfolioProjection()),
                new TransactionalSqlCommandExecutor(
                    new ConnectionStringSettings(
                        "projac",
                        @"Data Source=(localdb)\ProjectsV12;Initial Catalog=ProjacUsage;Integrated Security=SSPI;",
                        "System.Data.SqlClient"),
                    IsolationLevel.ReadCommitted));

            projector.Project(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 subscription = store.SubscribeToStream(stream, null, async(_, rawMessage) =>
            {
                var @event = JsonConvert.DeserializeObject(
                    await rawMessage.GetJsonData(),
                    Type.GetType(rawMessage.Type, true));

                projector.Project(@event);
            });
            //should complete within 5 seconds.
            await Task.Delay(TimeSpan.FromSeconds(5));

            subscription.Dispose();
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var projector = new SqlProjector(
                Resolve.WhenEqualToHandlerMessageType(new OnHandInventoryViewProjection()),
                new TransactionalSqlCommandExecutor(
                    SqlClientFactory.Instance,
                    @"Data Source=localhost;Initial Catalog=InventoryPOC;Integrated Security=SSPI;",
                    IsolationLevel.ReadCommitted));

            projector.Project(new List <object> {
                new DropSchema(), new CreateSchema()
            });

            var credentials = new UserCredentials("admin", "changeit");

            var connectionSettings = ConnectionSettings.Create()
                                     .KeepReconnecting()
                                     .KeepRetrying()
                                     .FailOnNoServerResponse()
                                     .WithConnectionTimeoutOf(TimeSpan.FromSeconds(20))
                                     .SetOperationTimeoutTo(TimeSpan.FromSeconds(10))
                                     .SetHeartbeatInterval(TimeSpan.FromSeconds(10))
                                     .SetHeartbeatTimeout(TimeSpan.FromSeconds(20));

            using (var connection = EventStoreConnection.Create("ConnectTo=tcp://admin:[email protected]:1113; HeartBeatTimeout=5000", connectionSettings))
            {
                connection.ConnectAsync().GetAwaiter().GetResult();

                var subscription = connection.SubscribeToStreamFrom("$ce-ledgerEntry", StreamPosition.Start, CatchUpSubscriptionSettings.Default, (_, @event) =>
                {
                    projector.Project(
                        JsonConvert.DeserializeObject(
                            Encoding.UTF8.GetString(@event.Event.Data),
                            Assembly.GetAssembly(typeof(PortfolioRenamed)).GetType(@event.Event.EventType)));
                }, userCredentials: credentials);

                var a = subscription.LastProcessedEventNumber;
                Console.ReadKey();
            }
        }
Esempio n. 4
0
        public async Task ShowWithStream()
        {
            //setup a projection schema (one of many ways)
            var projector = new SqlProjector(
                Resolve.WhenEqualToHandlerMessageType(new PortfolioProjection()),
                new TransactionalSqlCommandExecutor(
                    new ConnectionStringSettings(
                        "projac",
                        @"Data Source=(localdb)\ProjectsV12;Initial Catalog=ProjacUsage;Integrated Security=SSPI;",
                        "System.Data.SqlClient"),
                    IsolationLevel.ReadCommitted));

            projector.Project(new object[] { new DropSchema(), new CreateSchema() });

            //setup an embedded eventstore
            var node = EmbeddedVNodeBuilder.
                       AsSingleNode().
                       NoGossipOnPublicInterface().
                       NoStatsOnPublicInterface().
                       NoAdminOnPublicInterface().
                       OnDefaultEndpoints().
                       RunInMemory().
                       Build();

            node.Start();

            var connection = EmbeddedEventStoreConnection.Create(node);
            await connection.ConnectAsync();

            //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"));
            var credentials = new UserCredentials("admin", "changeit");
            await connection.AppendToStreamAsync(
                stream,
                ExpectedVersion.Any,
                events.Select(@event => new EventData(
                                  Guid.NewGuid(),
                                  @event.GetType().FullName,
                                  true,
                                  Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(@event)),
                                  new byte[0])).ToArray(),
                credentials);

            //project the sample stream (until end of stream)
            var result =
                await connection.ReadStreamEventsForwardAsync(stream, StreamPosition.Start, 1, false, credentials);

            while (!result.IsEndOfStream)
            {
                projector.Project(result.
                                  Events.
                                  Select(@event =>
                                         JsonConvert.DeserializeObject(
                                             Encoding.UTF8.GetString(@event.Event.Data),
                                             Type.GetType(@event.Event.EventType, true))));
                result =
                    await connection.ReadStreamEventsForwardAsync(stream, result.NextEventNumber, 1, false, credentials);
            }
            projector.Project(result.
                              Events.
                              Select(@event =>
                                     JsonConvert.DeserializeObject(
                                         Encoding.UTF8.GetString(@event.Event.Data),
                                         Type.GetType(@event.Event.EventType, true))));

            node.Stop();
        }
Esempio n. 5
0
 public void Initialize()
 {
     _projector.Project(new object[] { new DropSchema(), new CreateSchema() });
 }
Esempio n. 6
0
        public async Task ShowWithStream()
        {
            //setup a projection schema (one of many ways)
            var projector = new SqlProjector(
                Resolve.WhenEqualToHandlerMessageType(new PortfolioProjection()),
                new TransactionalSqlCommandExecutor(
                    new ConnectionStringSettings(
                        "projac",
                        @"Data Source=(localdb)\ProjectsV12;Initial Catalog=ProjacUsage;Integrated Security=SSPI;",
                        "System.Data.SqlClient"),
                    IsolationLevel.ReadCommitted));

            projector.Project(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));

                projector.Project(@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));

                    projector.Project(@event);
                }
            }
        }
Esempio n. 7
0
        public async void ShowWithCatchupSubscription()
        {
            //setup a projection schema (one of many ways)
            var projector = new SqlProjector(Instance.Handlers,
                                             new TransactionalSqlCommandExecutor(
                                                 new ConnectionStringSettings(
                                                     "projac",
                                                     @"Data Source=(localdb)\ProjectsV12;Initial Catalog=ProjacUsage;Integrated Security=SSPI;",
                                                     "System.Data.SqlClient"),
                                                 IsolationLevel.ReadCommitted));

            projector.Project(new object[] { new DropSchema(), new CreateSchema() });

            //setup an embedded eventstore
            var node = EmbeddedVNodeBuilder.
                       AsSingleNode().
                       NoGossipOnPublicInterface().
                       NoStatsOnPublicInterface().
                       NoAdminOnPublicInterface().
                       OnDefaultEndpoints().
                       RunInMemory().
                       Build();

            node.Start();

            var connection = EmbeddedEventStoreConnection.Create(node);
            await connection.ConnectAsync();

            //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"));
            var credentials = new UserCredentials("admin", "changeit");
            await connection.AppendToStreamAsync(
                stream,
                ExpectedVersion.Any,
                events.Select(@event => new EventData(
                                  Guid.NewGuid(),
                                  @event.GetType().FullName,
                                  true,
                                  Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(@event)),
                                  new byte[0])).ToArray(),
                credentials);

            //project the sample stream (until end of stream)
            var subscription = connection.SubscribeToStreamFrom(stream, StreamPosition.Start, false, (_, @event) =>
            {
                projector.Project(
                    JsonConvert.DeserializeObject(
                        Encoding.UTF8.GetString(@event.Event.Data),
                        Type.GetType(@event.Event.EventType, true)));
            }, userCredentials: credentials);
            //should complete within 5 seconds.
            await Task.Delay(TimeSpan.FromSeconds(5));

            subscription.Stop();

            node.Stop();
        }