Example #1
0
        /// <summary>
        /// Elimina un readmodel
        /// ?? gestire un flag di eliminato e memorizzare l'id dell'evento??
        /// </summary>
        /// <param name="e"></param>
        /// <param name="id"></param>
        /// <param name="notify"></param>
        public async Task DeleteAsync(DomainEvent e, TKey id, bool notify = false)
        {
            string[] topics = null;

            if (NotifySubscribers && typeof(ITopicsProvider).IsAssignableFrom(typeof(TModel)))
            {
                var model = await _storage.FindOneByIdAsync(id).ConfigureAwait(false);

                if (model == null)
                {
                    return;
                }

                topics = ((ITopicsProvider)model).GetTopics().ToArray();
            }

            var result = await _storage.DeleteAsync(id).ConfigureAwait(false);

            if (!result.Ok)
            {
                throw new CollectionWrapperException(FormatCollectionWrapperExceptionMessage(string.Format("Delete error on {0} :: {1}", typeof(TModel).FullName, id), e));
            }

            if (result.DocumentsAffected == 1 && ShouldSendNotification(e, notify))
            {
                await _notifyToSubscribers.Send(ReadModelUpdatedMessage.Deleted <TModel, TKey>(id, topics)).ConfigureAwait(false);
            }
        }
Example #2
0
        public async Task SaveAsync(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.Update);
            var orignalVersion = model.Version;

            model.Version++;
            model.LastModified = e.CommitStamp;
            model.AddEvent(e.MessageId);
            HandlePollableReadModel(e, model);
            var result = await _storage.SaveWithVersionAsync(model, orignalVersion).ConfigureAwait(false);

            if (!result.Ok)
            {
                throw new CollectionWrapperException(FormatCollectionWrapperExceptionMessage("Concurency exception", e));
            }

            if (ShouldSendNotification(e, notify))
            {
                Object notificationPayload = TransformForNotification(e, model);
                await _notifyToSubscribers.Send(ReadModelUpdatedMessage.Updated <TModel, TKey>(model.Id, notificationPayload)).ConfigureAwait(false);
            }
        }
        /// <summary>
        /// Elimina un readmodel
        /// ?? gestire un flag di eliminato e memorizzare l'id dell'evento??
        /// </summary>
        /// <param name="e"></param>
        /// <param name="id"></param>
        /// <param name="notify"></param>
        public void Delete(DomainEvent e, TKey id, bool notify = false)
        {
            string[] topics = null;

            if (NotifySubscribers && typeof(ITopicsProvider).IsAssignableFrom(typeof(TModel)))
            {
                var model = _storage.FindOneById(id);
                if (model == null)
                {
                    return;
                }

                topics = ((ITopicsProvider)model).GetTopics().ToArray();
            }

            var result = _storage.Delete(id);

            if (!result.Ok)
            {
                throw new Exception(string.Format("Delete error on {0} :: {1}", typeof(TModel).FullName, id));
            }

            if (result.DocumentsAffected == 1)
            {
                if (!IsReplay && (notify || NotifySubscribers))
                {
                    _notifyToSubscribers.Send(ReadModelUpdatedMessage.Deleted <TModel, TKey>(id, topics));
                }
            }
        }
        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);
            }
        }
        public void Save(DomainEvent e, TModel model, bool notify = false)
        {
            OnSave(model, e);
            var orignalVersion = model.Version;

            model.Version++;
            model.AddEvent(e.MessageId);
            model.LastModified = e.CommitStamp;

            var result = _storage.SaveWithVersion(model, orignalVersion);

            if (!result.Ok)
            {
                throw new Exception("Concurency exception");
            }

            if (!IsReplay && (notify || NotifySubscribers))
            {
                _notifyToSubscribers.Send(ReadModelUpdatedMessage.Updated <TModel, TKey>(PrepareForNotification(model)));
            }
        }
Example #6
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));
            }
        }