public static CommandEntity FromProperties(IReadOnlyDictionary <string, object> properties, RepositoryEntityMetadata metadata) { properties.GuardAgainstNull(nameof(properties)); metadata.GuardAgainstNull(nameof(metadata)); if (!properties.ContainsKey(nameof(Id)) || properties[nameof(Id)] == null) { throw new InvalidOperationException( "Unable to create a new CommandEntity. There is no 'Id' property in this collection of values."); } var result = new CommandEntity(properties[nameof(Id)].ToString()); foreach (var property in properties) { var propertyType = metadata.GetPropertyType(property.Key, false); if (propertyType != null) { result.Add(property.Key, property.Value, propertyType); } } return(result); }
public CommandEntity Add(string containerName, CommandEntity entity) { containerName.GuardAgainstNullOrEmpty(nameof(containerName)); entity.GuardAgainstNull(nameof(entity)); var container = EnsureContainer(containerName); container.Add(entity.Id, entity.ToFileProperties()); return(CommandEntity.FromCommandEntity( container.Get(entity.Id).FromFileProperties(entity.Metadata), entity)); }
public CommandEntity Add(string containerName, CommandEntity entity) { containerName.GuardAgainstNullOrEmpty(nameof(containerName)); entity.GuardAgainstNull(nameof(entity)); if (!this.containers.ContainsKey(containerName)) { this.containers.Add(containerName, new Dictionary <string, IReadOnlyDictionary <string, object> >()); } this.containers[containerName].Add(entity.Id, entity.ToDictionaryProperties()); return(CommandEntity.FromCommandEntity(this.containers[containerName][entity.Id].FromDictionaryProperties(), entity)); }
public CommandEntity Retrieve(string containerName, string id, RepositoryEntityMetadata metadata) { containerName.GuardAgainstNullOrEmpty(nameof(containerName)); id.GuardAgainstNullOrEmpty(nameof(id)); metadata.GuardAgainstNull(nameof(metadata)); var container = EnsureContainer(containerName); if (container.Exists(id)) { return(CommandEntity.FromCommandEntity( container.Get(id).FromFileProperties(metadata), metadata)); } return(default);
public CommandEntity Retrieve(string containerName, string id, RepositoryEntityMetadata metadata) { containerName.GuardAgainstNullOrEmpty(nameof(containerName)); id.GuardAgainstNullOrEmpty(nameof(id)); metadata.GuardAgainstNull(nameof(metadata)); if (this.containers.ContainsKey(containerName)) { if (this.containers[containerName].ContainsKey(id)) { return(CommandEntity.FromCommandEntity( this.containers[containerName][id].FromDictionaryProperties(), metadata)); } } return(default);
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(); }
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>()); }
public static CommandEntity FromCommandEntity(IReadOnlyDictionary <string, object> properties, CommandEntity descriptor) { return(FromCommandEntity(properties, descriptor.Metadata)); }