Esempio n. 1
0
        public async Task <IDisposable> AddSubscription <TEvent>(string sourceBoundedContextName,
                                                                 string publishedLanguageEntity)
            where TEvent : class
        {
            var queue = await _messageBus.QueueDeclareAsync(NamingConventions.QueueNamingConvention(_boundedContextName,
                                                                                                    sourceBoundedContextName, publishedLanguageEntity, _subscriberId));

            await _messageBus.BindAsync(
                new Exchange(
                    NamingConventions.ExchangeNamingConvention(sourceBoundedContextName, publishedLanguageEntity)),
                queue, "");

            return(_messageBus.Consume(queue, async(bytes, properties, info) =>
            {
                var msg = Encoding.UTF8.GetString(bytes);

                Console.WriteLine(msg);
                var validationResult = await _schemaValidator.IsValid <TEvent>(msg);
                if (validationResult.IsValid)
                {
                    var envelope = System.Text.Json.JsonSerializer.Deserialize <Envelope <TEvent> >(msg);
                    var props = new MessageProperties();
                    properties.CopyTo(props);
                    await _eventDispatcher.HandleEvent(envelope, props);
                }
                else
                {
                    throw new Exception($"Schema is invalid, errors: {string.Join(", ", validationResult.Errors)}");
                }
            }));
        }
Esempio n. 2
0
        private async Task OnMessage <TEvent>(IMessage message)
        {
            var msg      = (ITextMessage)message;
            var envelope = System.Text.Json.JsonSerializer.Deserialize <Envelope <TEvent> >(msg.Text);

            var props = new MessageProperties();
            await _eventDispatcher.HandleEvent(envelope, props);
        }
Esempio n. 3
0
        private void PerformPop(RecursiveStack <SessionEvents> mainStack, RecursiveStack <SessionEvents> altStack, SessionMode mode, int?toSavePoint)
        {
            var notify = false;

            lock (_sync)
            {
                int sid    = 0;
                var events = new List <IUndoableEvent>();
                using (var session = _store.BeginSession(new SessionConfiguration {
                    Mode = mode
                }))
                {
                    IEventDispatcher dispatcher      = null;
                    string           domainModelName = null;
                    while (mainStack.Count > 0)
                    {
                        if (toSavePoint != null && mainStack.Peek().SessionId == toSavePoint)
                        {
                            break;
                        }

                        var ci = mainStack.Pop();
                        foreach (var @event in Enumerable.Reverse(ci.Events))
                        {
                            var evt = @event.GetReverseEvent(session.SessionId);
                            if (evt == null)
                            {
                                continue;
                            }
                            if (domainModelName != evt.Domain)
                            {
                                dispatcher      = _domainModels[evt.Domain].Dispatcher;
                                domainModelName = evt.Domain;
                            }
                            dispatcher.HandleEvent(evt);

                            if (evt is IUndoableEvent)
                            {
                                events.Add(evt as IUndoableEvent);
                            }
                        }

                        sid = ci.SessionId;
                        if (toSavePoint == null)
                        {
                            break;
                        }
                    }

                    session.AcceptChanges();
                }

                if (events.Count > 0)
                {
                    notify = true;
                    altStack.Push(new SessionEvents
                    {
                        SessionId = sid,
                        Events    = events.ToList()
                    });
                }
            }

            if (notify)
            {
                OnPropertyChanged("CanUndo");
                OnPropertyChanged("CanRedo");
            }
        }