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); } }
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); }
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)); }
// 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()); }
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); }
public void Publish(IDomainEvent evt) { _eventStream.Add(evt); foreach (var handler in _eventHandlers) { handler.Handle(evt); } }
public void Publish <TEvent>(TEvent evt) where TEvent : IDomainEvent { _history.Add(evt); foreach (var subscriber in _subscribers.OfType <IHandler <TEvent> >()) { subscriber.Handle(evt); } }
/// <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); } }
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()); } }
private static void CommitOneValue(IEventStream stream, int value) { stream.Add(new EventMessage() { Body = new TestDomainEvent() { Value = value } }); stream.CommitChanges(Guid.NewGuid()); }
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); }
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()); } }
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()); } }
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()); } }
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); }
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()); } }
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()); } }
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()); } }
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()); } }
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(); }
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)); }
/// <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); } //} }
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()); } }
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>(); } } } }
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()); } }
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>(); } } } }
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(); }
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); }
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)); }