public async Task InsertAsync(DomainEvent e, TModel model, bool notify = false)
        {
            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }

            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            OnSave(model, e, CollectionWrapperOperationType.Insert);
            model.Version          = 1;
            model.AggregateVersion = e.Version;
            model.LastModified     = e.CommitStamp;

            model.AddEvent(e.MessageId);
            HandlePollableReadModel(e, model);
            try
            {
                var result = await _storage.InsertAsync(model).ConfigureAwait(false);

                if (!result.Ok)
                {
                    throw new CollectionWrapperException(FormatCollectionWrapperExceptionMessage("Error writing on mongodb :" + result.ErrorMessage, e));
                }
            }
            catch (MongoException ex)
            {
                if (!ex.Message.Contains(ConcurrencyException))
                {
                    throw;
                }

                var saved = await _storage.FindOneByIdAsync((dynamic)model.Id).ConfigureAwait(false);

                if (saved.BuiltFromEvent(e))
                {
                    return;
                }

                throw new CollectionWrapperException(FormatCollectionWrapperExceptionMessage("Readmodel created by two different events!", e));
            }

            if (ShouldSendNotification(e, notify))
            {
                Object notificationPayload = TransformForNotification(e, model);
                await _notifyToSubscribers.Send(ReadModelUpdatedMessage.Created <TModel, TKey>(model.Id, notificationPayload)).ConfigureAwait(false);
            }
        }
Пример #2
0
        public void Insert(DomainEvent e, TModel model, bool notify = false)
        {
            OnSave(model, e);
            model.Version = 1;
            model.AddEvent(e.MessageId);
            model.LastModified = e.CommitStamp;

            try
            {
                var result = _storage.Insert(model);

                if (!result.Ok)
                {
                    throw new Exception("Error writing on mongodb :" + result.ErrorMessage);
                }
            }
            catch (MongoException ex)
            {
                if (!ex.Message.Contains(ConcurrencyException))
                {
                    throw;
                }

                var saved = _storage.FindOneById((dynamic)model.Id);
                if (saved.BuiltFromEvent(e.MessageId))
                {
                    return;
                }

                throw new Exception("Readmodel created by two different events!");
            }

            if (!IsReplay && (notify || NotifySubscribers))
            {
                Object notificationPayload = null;
                notificationPayload = TransformForNotification(model);
                _notifyToSubscribers.Send(ReadModelUpdatedMessage.Created <TModel, TKey>(model.Id, notificationPayload));
            }
        }