public async Task DeleteByKeyAsync(TEntity specification, string etag, CancellationToken cancellationToken) { using (new SessionScope(SessionFactory)) { // First we must load the entity var persistedEntity = await _getEntityByKey.GetByKeyAsync(specification, cancellationToken); await DeleteAsync(persistedEntity, etag, cancellationToken); } }
/// <summary> /// Authorizes a call to get a single entity by its key. /// </summary> /// <param name="specification">An entity instance that has all the primary key properties assigned with values.</param> /// <returns>The specified entity if found; otherwise null.</returns> public async Task <T> GetByKeyAsync(T specification, CancellationToken cancellationToken) { // Pass the call through to the decorated repository method to get the entity // to allow the 404 "Not Found" exception to be detected prior to an authorization // exception. var entity = await _next.GetByKeyAsync(specification, cancellationToken); // Now that we know the entity exists, authorize access to it if (entity != null) { await AuthorizeSingleItemAsync(entity, cancellationToken); } return(entity); }
public async Task <UpsertEntityResult <TEntity> > UpsertAsync(TEntity entity, bool enforceOptimisticLock, CancellationToken cancellationToken) { using (new SessionScope(SessionFactory)) { var isCreated = false; var isModified = false; // Go try to get the existing entity TEntity persistedEntity = null; // Do we have an 'Id' value present? bool idHasValue = !entity.Id.Equals(default(Guid)); if (idHasValue) { // Look up by provided Id persistedEntity = await _getEntityById.GetByIdAsync(entity.Id, cancellationToken); // If attempt to look up by Id failed, don't allow create to proceed if Id was supplied by API client if (persistedEntity == null && entity.IdSource == IdentifierSource.ClientSupplied) { throw new NotFoundException("Resource to update was not found."); } } else { // Get it by primary key persistedEntity = await _getEntityByKey.GetByKeyAsync(entity, cancellationToken); } // If there is no existing entity... if (persistedEntity == null) { // Create the entity await _createEntity.CreateAsync(entity, enforceOptimisticLock, cancellationToken); persistedEntity = entity; isCreated = true; } else { // Update the entity if (enforceOptimisticLock) { if (!persistedEntity.LastModifiedDate.Equals(entity.LastModifiedDate)) { throw new ConcurrencyException("Resource was modified by another consumer."); } } // Synchronize using strongly-typed generated code isModified = entity.Synchronize(persistedEntity); // Force aggregate root to be touched with an updated date if aggregate has been modified if (isModified) { // Make root dirty, NHibernate will override the value during insert (through a hook) persistedEntity.LastModifiedDate = persistedEntity.LastModifiedDate.AddSeconds(1); } await _updateEntity.UpdateAsync(persistedEntity, cancellationToken); } return(new UpsertEntityResult <TEntity> { Entity = persistedEntity, IsCreated = isCreated, IsModified = isModified }); } }
public void When_invoking_the_service_the_decorator_should_pass_control_to_the_decorated_object() { resolvedIGetEntityByKey.GetByKeyAsync(new Student(), CancellationToken.None).GetResultSafely(); DecoratedGetEntityByKeyInstance.WasCalled.ShouldBeTrue(); }