Exemple #1
0
        private Task HandleProjectionDeletion(TKey key, NHibernateProjectionContext context,
                                              ProjectionDeletionOptions options)
        {
            TProjection existingProjection = context.Session.Get <TProjection>(key);

            if (existingProjection == null)
            {
                switch (options.MissingProjectionBehavior)
                {
                case MissingProjectionDeletionBehavior.Ignore:
                {
                    break;
                }

                case MissingProjectionDeletionBehavior.Throw:
                {
                    throw new ProjectionException(
                              $"Cannot delete {typeof(TProjection)} projection with key {key}. The projection does not exist.");
                }

                default:
                {
                    throw new NotSupportedException(
                              $"Not supported missing projection behavior {options.MissingProjectionBehavior}.");
                }
                }
            }
            else
            {
                context.Session.Delete(existingProjection);
            }

            return(Task.FromResult(false));
        }
        private async Task HandleProjectionDeletion(string key, RavenProjectionContext context,
                                                    ProjectionDeletionOptions options)
        {
            string databaseId = BuildDatabaseId(key);

            // If the projection is already loaded, we have to delete it via the loaded instance.
            // If the projection is not cached, we have to load it to verify that it exists.
            // Otherwise we can delete fast by id without loading the projection.
            if (context.Session.Advanced.IsLoaded(databaseId) || !await IsCached(databaseId).ConfigureAwait(false))
            {
                TProjection projection = await context.Session.LoadAsync <TProjection>(databaseId).ConfigureAwait(false);

                if (projection == null)
                {
                    switch (options.MissingProjectionBehavior)
                    {
                    case MissingProjectionDeletionBehavior.Ignore:
                    {
                        break;
                    }

                    case MissingProjectionDeletionBehavior.Throw:
                    {
                        throw new ProjectionException(
                                  $"Cannot delete projection with id {databaseId}. The projection does not exist.");
                    }

                    default:
                    {
                        throw new NotSupportedException(
                                  $"Not supported missing projection behavior {options.MissingProjectionBehavior}.");
                    }
                    }
                }
                else
                {
                    context.Session.Delete(projection);
                    cache.Remove(databaseId);
                }
            }
            else
            {
                context.Session.Delete(databaseId);
                cache.Remove(databaseId);
            }
        }
            public When_an_event_is_mapped_as_a_delete_if_exists()
            {
                Given(() =>
                {
                    var mapBuilder = new EventMapBuilder <ProductCatalogEntry, string, ProjectionContext>();
                    mapBuilder.Map <ProductDiscontinuedEvent>().AsDeleteIfExistsOf(e => e.ProductKey);

                    mapBuilder.HandleProjectionDeletionsAs((key, context, options) =>
                    {
                        projection = new ProductCatalogEntry
                        {
                            Id      = key,
                            Deleted = true
                        };

                        this.options = options;
                        return(Task.FromResult(0));
                    });

                    mapBuilder.HandleProjectionModificationsAs((key, context, projector, options) =>
                    {
                        throw new InvalidOperationException("Modification should not be called.");
                    });

                    mapBuilder.HandleCustomActionsAs((context, projector) =>
                    {
                        throw new InvalidOperationException("Custom action should not be called.");
                    });

                    map = mapBuilder.Build();
                });

                When(async() =>
                {
                    await map.Handle(
                        new ProductDiscontinuedEvent
                    {
                        ProductKey = "c350E"
                    },
                        new ProjectionContext());
                });
            }