示例#1
0
        protected virtual async Task <bool> SingleDeleteCommand(SingleQuery <TEntity> entityQuery, CancellationToken cancellationToken = default(CancellationToken))
        {
            var command = new EntityDeleteQuery <TEntity, TReadModel>(entityQuery, User);
            var result  = await Mediator.Send(command, cancellationToken).ConfigureAwait(false);

            return(result);
        }
        protected override async Task <bool> ProcessAsync(EntityDeleteQuery <TEntity, TReadModel> message, CancellationToken cancellationToken)
        {
            var entityQuery = message.Query;
            var dbSet       = _context.Set <TEntity>();
            // build query from filter
            var query = entityQuery.Query.Filter(dbSet);

            if (!string.IsNullOrEmpty(entityQuery.Query.IncludeProperties))
            {
                foreach (var includeProperty in entityQuery.Query.IncludeProperties.Split
                             (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    query = query.Include(includeProperty);
                }
            }

            // get total for query
            var total = await query
                        .CountAsync(cancellationToken)
                        .ConfigureAwait(false);

            // short circuit if total is zero
            if (total == 0)
            {
                throw new Exception("Unable to find records.,");
            }

            // page the query and convert to read model
            var result = await query
                         .FirstOrDefaultAsync(cancellationToken)
                         .ConfigureAwait(false);

            dbSet.Remove(result);
            var output = await _context
                         .SaveChangesAsync(cancellationToken)
                         .ConfigureAwait(false);

            return(output == 1 ? true:false);
        }