コード例 #1
0
        public void Add(T entity)
        {
            var newEvents = entity.FetchEvents();

            if (entity is IEntityProperties entityWithProperties)
            {
                var properties = entityWithProperties.GetProperties();

                _EventStream.Add(entity.Id, entity.GetType().Name, properties, newEvents);
            }
            else
            {
                _EventStream.Add(entity.Id, entity.GetType().Name, newEvents);
            }
        }
コード例 #2
0
ファイル: GameService.cs プロジェクト: angels2it/VirtualRisks
        public async Task AcceptedSelfPlayingGameAsync(string id)
        {
            var game = await Repository.GetByIdAsync(id);

            if (game == null)
            {
                throw new KeyNotFoundException();
            }
            var gameId = new Guid(id);

            using (IEventStream stream = _store.OpenStream(gameId))
            {
                stream.Add(new EventMessage
                {
                    Body = new ChangeGameStatusEvent
                    {
                        Status = GameStatus.Playing
                    }
                });
                stream.CommitChanges(Guid.NewGuid());
            }

            await AddSoldierForAcceptStep(gameId);

            await Repository.AcceptedSelfPlayingAsync(id);
        }
コード例 #3
0
        protected override Task <int> SaveChanges(ItemWithType[] newEvents,
                                                  TState currentState,
                                                  CancellationToken?cancellationToken)
        {
            for (var index = 0; index < newEvents.Length; index++)
            {
                eventStream.Add(new EventMessage()
                {
                    Body    = newEvents[index].instance,
                    Headers = new Dictionary <string, object>
                    {
                        { "EventType", newEvents[index].type }
                    }
                });
            }

            try
            {
                eventStream.CommitChanges(Guid.NewGuid());
            }

            catch (ConcurrencyException nce)
            {
                throw new Exceptions.ConcurrencyException(eventStream.StreamId, nce);
            }

            return(Task.FromResult(eventStream.StreamRevision));
        }
コード例 #4
0
 // Header of stream (like NEeventStore does). See: https://github.com/NEventStore/NEventStore/blob/master/src/NEventStore/CommonDomain/Persistence/EventStore/EventStoreRepository.cs#L197
 public static void AddMigrationCommit(this IEventStream stream, Type mementoType, object @event)
 {
     stream.UncommittedHeaders[AggregateTypeHeader] = mementoType.FullName.Replace("Memento", "");
     stream.Add(new EventMessage {
         Body = @event
     });
     stream.CommitChanges(Guid.NewGuid());
 }
コード例 #5
0
ファイル: GameService.cs プロジェクト: angels2it/VirtualRisks
        public async Task AcceptedAsync(string id, string userId, string heroId = "")
        {
            var game = await Repository.GetByIdAsync(id);

            if (game == null)
            {
                throw new KeyNotFoundException("Game.NotFound");
            }
            Task userTask = !string.IsNullOrEmpty(heroId) ? Task.FromResult(true) : _userRepository.GetByIdAsync(userId).ContinueWith(r =>
            {
                User result = r.Result;
                if (result?.Heroes != null && result.Heroes.Count > 0)
                {
                    heroId = result.Heroes[0].Id;
                }
            });
            await Task.WhenAll(userTask);

            var gameId = new Guid(id);

            using (IEventStream stream = _store.OpenStream(gameId))
            {
                stream.Add(new EventMessage
                {
                    Body = new UpdateOpponentInfoEvent
                    {
                        OpponentId     = userId,
                        OpponentHeroId = heroId
                    }
                });
                stream.Add(new EventMessage
                {
                    Body = new ChangeGameStatusEvent
                    {
                        Status = GameStatus.Playing
                    }
                });

                stream.CommitChanges(Guid.NewGuid());
            }

            await AddSoldierForAcceptStep(gameId);

            await Repository.AcceptedAsync(id, userId, heroId);
        }
コード例 #6
0
        public void Publish(IDomainEvent evt)
        {
            _eventStream.Add(evt);

            foreach (var handler in _eventHandlers)
            {
                handler.Handle(evt);
            }
        }
コード例 #7
0
ファイル: EventBus.cs プロジェクト: dublow/CqrsRadio
        public void Publish <TEvent>(TEvent evt) where TEvent : IDomainEvent
        {
            _history.Add(evt);

            foreach (var subscriber in _subscribers.OfType <IHandler <TEvent> >())
            {
                subscriber.Handle(evt);
            }
        }
コード例 #8
0
 /// <summary>
 /// Write a new event to the event store
 /// </summary>
 /// <param name="streamId"></param>
 /// <param name="command"></param>
 public void AppendToStream(Guid streamId, Command command)
 {
     using (IEventStream stream = _store.OpenStream(streamId, 0, int.MaxValue))
     {
         stream.Add(new EventMessage {
             Body = command
         });
         stream.CommitChanges(command.EventId);
     }
 }
コード例 #9
0
 public void AppendToStream(IEvent @event)
 {
     using (IEventStream stream = _store.OpenStream(@event.Guid, 0, int.MaxValue))
     {
         stream.Add(new EventMessage {
             Body = @event
         });
         stream.CommitChanges(Guid.NewGuid());
     }
 }
コード例 #10
0
 private static void CommitOneValue(IEventStream stream, int value)
 {
     stream.Add(new EventMessage()
     {
         Body = new TestDomainEvent()
         {
             Value = value
         }
     });
     stream.CommitChanges(Guid.NewGuid());
 }
コード例 #11
0
        public async Task Append(EventPayload evnt)
        {
            var json = JsonConvert.SerializeObject(evnt.Data);

            _eventStream.Add(evnt.Subject,
                             evnt.Aggregate,
                             evnt.Kind,
                             json,
                             evnt.Timestamp);

            await _coordinatorApi.Publish(evnt);
        }
コード例 #12
0
        private static void OnSoldierCreateEvent(SoldierCreatedEvent createSoldier)
        {
            using (IEventStream stream = _store.OpenStream(_castleId))
            {
                var @event = new CreateSoldierEvent(Guid.NewGuid(), ProductionTime);

                stream.Add(new EventMessage {
                    Body = @event
                });
                stream.CommitChanges(Guid.NewGuid());
            }
        }
コード例 #13
0
        public void LoadFromSnapshotForwardAndAppend(IEvent @event)
        {
            var latestSnapshot = _store.Advanced.GetSnapshot(@event.Guid, 0);

            using (IEventStream stream = _store.OpenStream(latestSnapshot, int.MaxValue))
            {
                stream.Add(new EventMessage {
                    Body = @event
                });
                stream.CommitChanges(Guid.NewGuid());
            }
        }
コード例 #14
0
 public void AppendToStream(IList <IEvent> @events)
 {
     using (IEventStream stream = _store.OpenStream(@events.FirstOrDefault().Guid, 0, int.MaxValue))
     {
         foreach (var @event in @events)
         {
             stream.Add(new EventMessage {
                 Body = @event
             });
         }
         stream.CommitChanges(Guid.NewGuid());
     }
 }
コード例 #15
0
 private static void CreateGame()
 {
     using (IEventStream stream = _store.OpenStream(_gameId, 0))
     {
         // create game
         var @event = new CreateGameEvent();
         stream.Add(new EventMessage {
             Body = @event
         });
         stream.CommitChanges(Guid.NewGuid());
     }
     CreateCastle(_castleId);
 }
コード例 #16
0
        private static void CreateSoldier()
        {
            Console.WriteLine("Create soldier");
            using (IEventStream stream = _store.OpenStream(_castleId, 0))
            {
                var @event = new CreateSoldierEvent(Guid.NewGuid(), ProductionTime);

                stream.Add(new EventMessage {
                    Body = @event
                });
                stream.CommitChanges(Guid.NewGuid());
            }
        }
コード例 #17
0
 public void OpenOrCreateStream(IEvent @event)
 {
     // we can call CreateStream(StreamId) if we know there isn't going to be any data.
     // or we can call OpenStream(StreamId, 0, int.MaxValue) to read all commits,
     // if no commits exist then it creates a new stream for us.
     using (IEventStream stream = _store.OpenStream(@event.Guid, 0, int.MaxValue))
     {
         stream.Add(new EventMessage {
             Body = @event
         });
         stream.CommitChanges(Guid.NewGuid());
     }
 }
コード例 #18
0
 public void AddEvent(Guid streamId, params EventBase[] @events)
 {
     using (IEventStream eventStream = _store.OpenStream(streamId))
     {
         foreach (var @event in events)
         {
             eventStream.Add(new EventMessage()
             {
                 Body = @event
             });
         }
         eventStream.CommitChanges(Guid.NewGuid());
     }
 }
コード例 #19
0
        private static void AppendToStream()
        {
            using (IEventStream stream = store.OpenStream(StreamId, int.MinValue, int.MaxValue))
            {
                var @event = new SomeDomainEvent {
                    Value = "Second event."
                };

                stream.Add(new EventMessage {
                    Body = @event
                });
                stream.CommitChanges(Guid.NewGuid());
            }
        }
コード例 #20
0
 private static void CreateCastle(Guid castleId)
 {
     Console.WriteLine("Create castle");
     using (IEventStream stream = _store.OpenStream(castleId, 0))
     {
         // create game
         var @event = new CreateCastleEvent();
         stream.Add(new EventMessage {
             Body = @event
         });
         stream.CommitChanges(Guid.NewGuid());
     }
     CreateSoldier();
 }
コード例 #21
0
ファイル: EventStore.cs プロジェクト: umfaruki/CashLady
        public void SaveEvents(Guid aggregateId, IEnumerable <Event> events, int expectedVersion)
        {
            using (IEventStream stream = store.OpenStream(aggregateId, int.MinValue, int.MaxValue))
            {
                foreach (var @event in events)
                {
                    stream.Add(new EventMessage {
                        Body = @event
                    });
                }

                stream.CommitChanges(Guid.NewGuid());
            }
        }
        private Task PopulateMaster()
        {
            IEventStream stream = _master.CreateStream("tweets");

            IEnumerable <Task> tasks = ReadTweets().Select(tweet =>
            {
                var message = new TweetMessage(tweet);
                stream.Add(new EventMessage {
                    Body = message
                });
                return(stream.CommitChanges(Guid.NewGuid()));
            });

            return(Task.WhenAll(tasks));
        }
コード例 #23
0
 /// <summary>
 /// Write event to the stream(RavenDB collection)
 /// </summary>
 /// <param name="event"></param>
 private void OpenOrCreateStream(object @event)
 {
     //if (!ReplayService.ReplayMode)
     //{
     _stream.Add(new EventMessage {
         Body = @event
     });
     _stream.CommitChanges(Guid.NewGuid());
     if (@event is Order)
     {
         Log.Debug("New order stored in Event Store. ID: " + (@event as Order).OrderId.Id +
                   " | Order state: "
                   + (@event as Order).OrderState);
     }
     //}
 }
コード例 #24
0
        private static void LoadFromSnapshotForwardAndAppend()
        {
            Snapshot latestSnapshot = store.Advanced.GetSnapshot(StreamId, int.MaxValue);

            using (IEventStream stream = store.OpenStream(latestSnapshot, int.MaxValue))
            {
                var @event = new SomeDomainEvent {
                    Value = "Third event (first one after a snapshot)."
                };

                stream.Add(new EventMessage {
                    Body = @event
                });
                stream.CommitChanges(Guid.NewGuid());
            }
        }
コード例 #25
0
        public async Task When_new_commit_then_should_dispatch()
        {
            using (IStoreEvents eventStore = Wireup.Init().UsingInMemoryPersistence().Build())
            {
                var dispatchedEvents = new List <EventMessage <TestEvent> >();
                var handlerModule    = new TestHandlerModule(dispatchedEvents);

                using (var host = new DurableCommitDispatcher(
                           new EventStoreClient(eventStore.Advanced),
                           new InMemoryCheckpointRepository(),
                           new HandlerResolver(handlerModule)))
                {
                    var projectedCommits = host
                                           .ProjectedCommits
                                           .Replay();

                    using (projectedCommits.Connect())
                    {
                        await host.Start();

                        var streamId = Guid.NewGuid().ToString().FormatStreamIdWithBucket();

                        Guid commitId = Guid.NewGuid();

                        Task <ICommit> commitProjected = projectedCommits
                                                         .Take(1)
                                                         .ToTask();

                        using (IEventStream stream = eventStore.CreateStream(streamId))
                        {
                            stream.Add(new global::NEventStore.EventMessage {
                                Body = new TestEvent()
                            });
                            stream.CommitChanges(commitId);
                        }
                        host.PollNow();
                        await commitProjected;

                        dispatchedEvents.Count.Should().Be(1);
                        dispatchedEvents[0].Commit().Should().NotBeNull();
                        dispatchedEvents[0].Headers.Should().NotBeNull();
                        dispatchedEvents[0].Version.Should().Be(1);
                        dispatchedEvents[0].DomainEvent.Should().BeOfType <TestEvent>();
                    }
                }
            }
        }
コード例 #26
0
        private static void OpenOrCreateStream()
        {
            // we can call CreateStream(StreamId) if we know there isn't going to be any data.
            // or we can call OpenStream(StreamId, 0, int.MaxValue) to read all commits,
            // if no commits exist then it creates a new stream for us.
            using (IEventStream stream = store.OpenStream(StreamId, 0, int.MaxValue))
            {
                var @event = new SomeDomainEvent {
                    Value = "Initial event."
                };

                stream.Add(new EventMessage {
                    Body = @event
                });
                stream.CommitChanges(Guid.NewGuid());
            }
        }
コード例 #27
0
        public async Task When_handler_throws_Then_invoke_exception_callback()
        {
            using (IStoreEvents eventStore = Wireup.Init().UsingInMemoryPersistence().Build())
            {
                var projectedEvents = new List <EventMessage <TestEvent> >();
                var handlerModule   = new TestHandlerModule(projectedEvents);

                using (var host = new DurableCommitDispatcher(
                           new EventStoreClient(eventStore.Advanced),
                           new InMemoryCheckpointRepository(),
                           new HandlerResolver(handlerModule)))
                {
                    var projectedCommits = host
                                           .ProjectedCommits
                                           .Replay();

                    using (projectedCommits.Connect())
                    {
                        await host.Start();

                        var streamId = Guid.NewGuid().ToString().FormatStreamIdWithBucket();

                        Guid commitId = Guid.NewGuid();

                        Task <ICommit> commitProjected = projectedCommits
                                                         .Take(1)
                                                         .ToTask();

                        using (IEventStream stream = eventStore.CreateStream(streamId))
                        {
                            stream.Add(new global::NEventStore.EventMessage {
                                Body = new TestEventThatThrows()
                            });
                            stream.CommitChanges(commitId);
                        }
                        host.PollNow();

                        Func <Task> act = async() => await commitProjected;

                        act.ShouldThrow <Exception>();
                    }
                }
            }
        }
コード例 #28
0
        protected override void Because()
        {
            _moreThanPageSize = ConfiguredPageSizeForTesting + 1;
            var eventStore = new OptimisticEventStore(Persistence, null);

            // TODO: Not sure how to set the actual pagesize to the const defined above
            for (int i = 0; i < _moreThanPageSize; i++)
            {
                using (IEventStream stream = eventStore.OpenStream(Guid.NewGuid()))
                {
                    stream.Add(new EventMessage {
                        Body = i
                    });
                    stream.CommitChanges(Guid.NewGuid());
                }
            }
            ICommit[] commits = Persistence.GetFrom(DateTime.MinValue).ToArray();
            _commits = Persistence.GetFrom().ToArray();
        }
コード例 #29
0
ファイル: GameService.cs プロジェクト: angels2it/VirtualRisks
        public async Task SetOpponentArmySettingAsync(string id, GameArmySettingModel armySetting)
        {
            var game = await Repository.GetByIdAsync(id);

            if (game == null)
            {
                throw new KeyNotFoundException();
            }
            var opponentArmySetting = Mapper.Map <GameArmySetting>(armySetting);

            using (IEventStream stream = _store.OpenStream(new Guid(id)))
            {
                stream.Add(new EventMessage
                {
                    Body = new SelectOpponentArmySettingEvent
                    {
                        ArmySetting = opponentArmySetting
                    }
                });
            }
            await Repository.SetOpponentArmySettingAsync(id, opponentArmySetting);
        }
コード例 #30
0
        protected override Task <int> SaveChanges(object[] newEvents,
                                                  TState currentState,
                                                  CancellationToken?cancellationToken)
        {
            for (var index = 0; index < newEvents.Length; index++)
            {
                eventStream.Add(new EventMessage()
                {
                    Body = newEvents[index]
                });
            }

            try
            {
                eventStream.CommitChanges(Guid.NewGuid());
            }
            catch (ConcurrencyException nce)
            {
                throw new Exceptions.ConcurrencyException(eventStream.StreamId, nce);
            }

            return(Task.FromResult(eventStream.StreamRevision));
        }