Example #1
0
        public TDto Create(string id, Action <TDto> action = null)
        {
            id.GuardAgainstNullOrEmpty(nameof(id));

            var dto = new TDto {
                Id = id
            };

            action?.Invoke(dto);

            var entity = this.repository.Add(ContainerName, CommandEntity.FromType(dto));

            this.logger.LogDebug("Created new read model for entity {Id}", id);

            return(entity.ToReadModelEntity <TDto>());
        }
        public void Save(TAggregateRoot aggregate)
        {
            aggregate.GuardAgainstNull(nameof(aggregate));

            if (!aggregate.Id.HasValue() || aggregate.Id.IsEmpty())
            {
                throw new ResourceConflictException(Resources.GeneralEventStreamStorage_SaveWithAggregateIdMissing);
            }

            VerifyConcurrencyCheck(aggregate);

            var events = aggregate.GetChanges();

            if (!events.Any())
            {
                return;
            }

            var eventContainerName = GetEventContainerName();

            events.ForEach(change => { this.repository.Add(eventContainerName, CommandEntity.FromType(change)); });

            if (OnEventStreamStateChanged != null)
            {
                var changes = events
                              .Select(ToStateChange)
                              .ToList();
                try
                {
                    OnEventStreamStateChanged(this, new EventStreamStateChangedArgs(changes));
                }
                catch (Exception ex)
                {
                    //Ignore exception and continue
                    this.recorder.TraceError(ex, Resources.GeneralEventStreamStorage_SaveEventRelayFailed.Fmt(ex));
                }
            }

            aggregate.ClearChanges();
        }
Example #3
0
        public TDto Update(string id, Action <TDto> action)
        {
            id.GuardAgainstNullOrEmpty(nameof(id));
            action.GuardAgainstNull(nameof(action));

            var entity = this.repository.Retrieve(ContainerName, id,
                                                  RepositoryEntityMetadata.FromType <TDto>());

            if (entity == null)
            {
                throw new ResourceNotFoundException();
            }

            var dto = entity.ToReadModelEntity <TDto>();

            action(dto);
            var updated = this.repository.Replace(ContainerName, id, CommandEntity.FromType(dto));

            this.logger.LogDebug("Updated read model for entity {Id}", id);

            return(updated.ToReadModelEntity <TDto>());
        }