public void Delete(Identifier id, bool destroy = true) { id.GuardAgainstNull(nameof(id)); var entity = this.repository.Retrieve(this.containerName, id, RepositoryEntityMetadata.FromType <TEntity>()); if (entity == null) { return; } if (destroy) { this.repository.Remove(this.containerName, id); this.recorder.TraceDebug("Entity {Id} was destroyed in repository", id); return; } if (entity.IsDeleted.GetValueOrDefault(false)) { return; } entity.IsDeleted = true; this.repository.Replace(this.containerName, id, entity); this.recorder.TraceDebug("Entity {Id} was soft-deleted in repository", id); }
public TEntity ResurrectDeleted(Identifier id) { var entity = this.repository.Retrieve(this.containerName, id, RepositoryEntityMetadata.FromType <TEntity>()); if (entity == null) { return(default);
private static QueryEntity FromProperties <TType>(IReadOnlyDictionary <string, object> properties) where TType : IQueryableEntity { properties.GuardAgainstNull(nameof(properties)); var metadata = RepositoryEntityMetadata.FromType <TType>(); return(FromProperties(properties, metadata)); }
public TDtoWithId Get <TDtoWithId>(Identifier id, bool includeDeleted = false) where TDtoWithId : IQueryableEntity, IHasIdentity, new() { id.GuardAgainstNull(nameof(id)); var entity = this.repository.Retrieve(this.containerName, id, RepositoryEntityMetadata.FromType <TDto>()); if (entity == null) { return(default);
public TDtoWithId Get <TDtoWithId>(Identifier id) where TDtoWithId : IQueryableEntity, IHasIdentity, new() { id.GuardAgainstNull(nameof(id)); var entity = this.repository.Retrieve(this.containerName, id, RepositoryEntityMetadata.FromType <TDto>()); this.logger.LogDebug($"Entity {id} was retrieved from repository"); return(entity == null ? default : entity.ToQueryDto <TDtoWithId>()); }
public TEntity Get(Identifier id) { id.GuardAgainstNull(nameof(id)); var entity = this.repository.Retrieve(this.containerName, id, RepositoryEntityMetadata.FromType <TEntity>()); this.logger.LogDebug("Entity {Id} was retrieved from repository", id); return(entity != null ? entity.ToDomainEntity <TEntity>(this.domainFactory) : default);
public QueryResults <TDto> Query(QueryClause <TDto> query) { if (query == null || query.Options.IsEmpty) { this.logger.LogDebug("No entities were retrieved from repository, the query is empty"); return(new QueryResults <TDto>(new List <TDto>())); } var entities = this.repository.Query(this.containerName, query, RepositoryEntityMetadata.FromType <TDto>()); this.logger.LogDebug($"{entities.Count} Entities were retrieved from repository"); return(new QueryResults <TDto>(entities.ConvertAll(x => x.ToEntity <TDto>(this.domainFactory)))); }
public QueryResults <TDto> Query(QueryClause <TDto> query, bool includeDeleted = false) { if (query == null || query.Options.IsEmpty) { this.recorder.TraceDebug("No entities were retrieved from repository, the query is empty"); return(new QueryResults <TDto>(new List <TDto>())); } var entities = this.repository.Query(this.containerName, query, RepositoryEntityMetadata.FromType <TDto>()); entities = entities .Where(e => !e.IsDeleted.GetValueOrDefault(false) || includeDeleted) .ToList(); this.recorder.TraceDebug($"{entities.Count} Entities were retrieved from repository"); return(new QueryResults <TDto>(entities.ConvertAll(x => x.ToEntity <TDto>(this.domainFactory)))); }
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>()); }
private void VerifyConcurrencyCheck(TAggregateRoot aggregate) { var streamName = GetEventStreamName(aggregate.Id); var eventContainerName = GetEventContainerName(); var query = Query.From <EntityEvent>().Where(ee => ee.StreamName, ConditionOperator.EqualTo, streamName) .OrderBy(ee => ee.Version).Take(1); var latestEvent = this.repository .Query(eventContainerName, query, RepositoryEntityMetadata.FromType <EntityEvent>()) .FirstOrDefault(); var latestStoredVersion = latestEvent == null ? 0 : latestEvent.ToEntity <EntityEvent>(this.domainFactory).Version; var loadedVersion = aggregate.ChangeVersion; if (latestStoredVersion > loadedVersion) { throw new ResourceConflictException( Resources.GeneralEventStreamStorage_LoadConcurrencyConflictWritingEventStream.Fmt(streamName, loadedVersion)); } }
private static string FirstProperty <TQueryableEntity>() where TQueryableEntity : IQueryableEntity { var metadata = RepositoryEntityMetadata.FromType <TQueryableEntity>(); return(metadata.Types.First().Key); }
private static bool HasProperty <TQueryableEntity>(string propertyName) where TQueryableEntity : IQueryableEntity { var metadata = RepositoryEntityMetadata.FromType <TQueryableEntity>(); return(metadata.HasType(propertyName)); }
public TAggregateRoot Load(Identifier id, bool returnNullIfNotFound = false) { id.GuardAgainstNull(nameof(id)); var streamName = GetEventStreamName(id); var eventContainerName = GetEventContainerName(); var events = this.repository.Query(eventContainerName, Query.From <EntityEvent>() .Where(ee => ee.StreamName, ConditionOperator.EqualTo, streamName) .OrderBy(ee => ee.LastPersistedAtUtc), RepositoryEntityMetadata.FromType <EntityEvent>()); if (!events.Any()) { return(returnNullIfNotFound ? default : RehydrateAggregateRoot(id, null)); } var lastPersistedAtUtc = events.Last().LastPersistedAtUtc; var aggregate = RehydrateAggregateRoot(id, lastPersistedAtUtc); aggregate.LoadChanges(events.ConvertAll(@event => @event.ToEntity <EntityEvent>(this.domainFactory)), this.migrator); return(aggregate); }