ardalis / Specification

Base class with tests for adding specifications to a DDD model
MIT License
1.84k stars 240 forks source link

Feature Request: Deletion of entities via specification #363

Closed thorstenalpers closed 9 months ago

thorstenalpers commented 10 months ago

Hi, Ardalis Specification is a really great project that I use extensively as Nuget. I have an additional feature request. I have implemented in my own EfRepository, but would like to see in your project. It is about deleting entities per specification.

The existing way is to list entities and then delete them.

 public abstract class RepositoryBase<T> : IReadRepository<T> where T : class
{
...
   public virtual async Task DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
    {
        var query = ApplySpecification(specification);
        _dbContext.Set<T>().RemoveRange(query);
        await SaveChangesAsync(cancellationToken);
    }
...
}

I could also create a PR with unittests.

fiseni commented 10 months ago

Hi @thorstenalpers

Yea we may add it. Do you want to give it a try? @ardalis do you have any objections?

thorstenalpers commented 9 months ago

Yeah, I would try it out, I could also create a PR with UnitTests.

ardalis commented 9 months ago

No objections, go for it @thorstenalpers !

thorstenalpers commented 9 months ago

@ardalis I have created a fork and created a branch "feature/delete-range-by-spec" and created a PR in my repository.

PR: https://github.com/ardalis/Specification/pull/369

I have implemented it for both EF6 and EfCore. EF6 doesn't delete referenced entities automatically, therefore I added some specifications for entities without referenced entities.

If you want some more tests or explanations then feel free to contact me.

fiseni commented 9 months ago

Resolved by PR #369