Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Talpers/delete range by spec #369

Merged
merged 4 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ public virtual async Task DeleteRangeAsync(IEnumerable<T> entities, Cancellation
{
_dbContext.Set<T>().RemoveRange(entities);

await SaveChangesAsync(cancellationToken);
}

/// <inheritdoc/>
public virtual async Task DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
{
var query = ApplySpecification(specification);
_dbContext.Set<T>().RemoveRange(query);

await SaveChangesAsync(cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xunit;

namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture.Collections;

[CollectionDefinition("WriteCollection")]
public class WriteCollection : ICollectionFixture<DatabaseFixture>
{
public WriteCollection()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ public class Repository<T> : RepositoryBase<T> where T : class
{
protected readonly TestDbContext dbContext;

public Repository(TestDbContext dbContext) : base(dbContext)
public Repository(TestDbContext dbContext) : this(dbContext, SpecificationEvaluator.Default)
{
}

public Repository(TestDbContext dbContext, ISpecificationEvaluator specificationEvaluator) : base(dbContext, specificationEvaluator)
{
this.dbContext = dbContext;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture;
using Ardalis.Specification.UnitTests.Fixture.Entities;
using Ardalis.Specification.UnitTests.Fixture.Specs;
using FluentAssertions;
using System.Threading.Tasks;
using Xunit;

namespace Ardalis.Specification.EntityFramework6.IntegrationTests;

[Collection("WriteCollection")]
public class RepositoryOfT_DeleteRangeAsync
{
private readonly string _connectionString;

public RepositoryOfT_DeleteRangeAsync(DatabaseFixture fixture)
{
_connectionString = fixture.ConnectionString;
}

[Fact]
public virtual async Task DeletesProductWithStoreIdOne_GivenProductByStoreIdSpec()
{
using var dbContext = new TestDbContext(_connectionString);
var repo = new Repository<Product>(dbContext, SpecificationEvaluator.Default);

await repo.DeleteRangeAsync(new ProductByStoreIdSpec(1));

var products = await repo.ListAsync();
products.Should().NotBeNullOrEmpty();
products.Should().NotContain(e => e.StoreId == 1);
}

[Fact]
public virtual async Task DeletesProductWithIdOne_GivenProductByIdSpec()
{
using var dbContext = new TestDbContext(_connectionString);
var repo = new Repository<Product>(dbContext, SpecificationEvaluator.Default);

await repo.DeleteRangeAsync(new ProductByIdSpec(2));

var products = await repo.ListAsync();
products.Should().NotBeNullOrEmpty();
products.Should().NotContain(e => e.Id == 2);
}

[Fact]
public virtual async Task DeletesProductWithIdOne_GivenProductByIdAsUntrackedWithIdentityResolutionSpec()
{
using var dbContext = new TestDbContext(_connectionString);
var repo = new Repository<Product>(dbContext, SpecificationEvaluator.Default);

await repo.DeleteRangeAsync(new ProductByIdAsUntrackedWithIdentityResolutionSpec(3));

var products = await repo.ListAsync();
products.Should().NotBeNullOrEmpty();
products.Should().NotContain(e => e.Id == 3);
}

[Fact]
public virtual async Task DeletesProductWithIdOne_GivenProductByIdAsTrackedSpec()
{
using var dbContext = new TestDbContext(_connectionString);
var repo = new Repository<Product>(dbContext, SpecificationEvaluator.Default);

await repo.DeleteRangeAsync(new ProductByIdAsTrackedSpec(4));

var products = await repo.ListAsync();
products.Should().NotBeNullOrEmpty();
products.Should().NotContain(e => e.Id == 4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ public async Task DeleteRangeAsync(IEnumerable<TEntity> entities, CancellationTo
dbContext.Set<TEntity>().RemoveRange(entities);

await SaveChangesAsync(dbContext, cancellationToken);
}

/// <inheritdoc/>
public async Task DeleteRangeAsync(ISpecification<TEntity> specification, CancellationToken cancellationToken = default)
{
await using var dbContext = _dbContextFactory.CreateDbContext();
var query = ApplySpecification(specification, dbContext);
dbContext.Set<TEntity>().RemoveRange(query);

await SaveChangesAsync(cancellationToken);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,22 @@ public virtual async Task DeleteAsync(T entity, CancellationToken cancellationTo
_dbContext.Set<T>().Remove(entity);

await SaveChangesAsync(cancellationToken);
}

}
/// <inheritdoc/>
public virtual async Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default)
{
_dbContext.Set<T>().RemoveRange(entities);

await SaveChangesAsync(cancellationToken);
}

/// <inheritdoc/>
public virtual async Task DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
{
var query = ApplySpecification(specification);
_dbContext.Set<T>().RemoveRange(query);

await SaveChangesAsync(cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xunit;

namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture.Collections;

[CollectionDefinition("WriteCollection")]
public class WriteCollection : ICollectionFixture<DatabaseFixture>
{
public WriteCollection()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture;
using Ardalis.Specification.UnitTests.Fixture.Entities;
using Ardalis.Specification.UnitTests.Fixture.Specs;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Xunit;

namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests;

[Collection("WriteCollection")]
public class RepositoryOfT_DeleteRangeAsync : RepositoryOfT_DeleteRangeAsync_TestKit
{
public RepositoryOfT_DeleteRangeAsync(DatabaseFixture fixture) : base(fixture, SpecificationEvaluator.Default)
{
}
}

public abstract class RepositoryOfT_DeleteRangeAsync_TestKit
{
private readonly DbContextOptions<TestDbContext> _dbContextOptions;
private readonly ISpecificationEvaluator _specificationEvaluator;

protected RepositoryOfT_DeleteRangeAsync_TestKit(DatabaseFixture fixture, ISpecificationEvaluator specificationEvaluator)
{
_dbContextOptions = fixture.DbContextOptions;
_specificationEvaluator = specificationEvaluator;
}

[Fact]
public virtual async Task DeletesProductWithStoreIdOne_GivenProductByStoreIdSpec()
{
using var dbContext = new TestDbContext(_dbContextOptions);
var repo = new Repository<Product>(dbContext, _specificationEvaluator);

await repo.DeleteRangeAsync(new ProductByStoreIdSpec(1));

var products = await repo.ListAsync();
products.Should().NotBeNullOrEmpty();
products.Should().NotContain(e => e.StoreId == 1);
}

[Fact]
public virtual async Task DeletesProductWithIdOne_GivenProductByIdSpec()
{
using var dbContext = new TestDbContext(_dbContextOptions);
var repo = new Repository<Product>(dbContext, _specificationEvaluator);

await repo.DeleteRangeAsync(new ProductByIdSpec(2));

var products = await repo.ListAsync();
products.Should().NotBeNullOrEmpty();
products.Should().NotContain(e => e.Id == 2);
}

[Fact]
public virtual async Task DeletesProductWithIdOne_GivenProductByIdAsUntrackedWithIdentityResolutionSpec()
{
using var dbContext = new TestDbContext(_dbContextOptions);
var repo = new Repository<Product>(dbContext, _specificationEvaluator);

await repo.DeleteRangeAsync(new ProductByIdAsUntrackedWithIdentityResolutionSpec(3));

var products = await repo.ListAsync();
products.Should().NotBeNullOrEmpty();
products.Should().NotContain(e => e.Id == 3);
}

[Fact]
public virtual async Task DeletesProductWithIdOne_GivenProductByIdAsTrackedSpec()
{
using var dbContext = new TestDbContext(_dbContextOptions);
var repo = new Repository<Product>(dbContext, _specificationEvaluator);

await repo.DeleteRangeAsync(new ProductByIdAsTrackedSpec(4));

var products = await repo.ListAsync();
products.Should().NotBeNullOrEmpty();
products.Should().NotContain(e => e.Id == 4);
}
}
10 changes: 9 additions & 1 deletion Specification/src/Ardalis.Specification/IRepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ public interface IRepositoryBase<T> : IReadRepositoryBase<T> where T : class
/// </summary>
/// <param name="entities">The entities to remove.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default);
Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default);

/// <summary>
/// Removes the all entities of <typeparamref name="T" />, that matches the encapsulated query logic of the
/// <paramref name="specification"/>, from the database.
/// </summary>
/// <param name="specification">The encapsulated query logic.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default);

/// <summary>
/// Persists changes to the database.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Ardalis.Specification.UnitTests.Fixture.Specs;

public class ProductByIdAsTrackedSpec : Specification<Product>, ISingleResultSpecification<Product>
{
public ProductByIdAsTrackedSpec(int id)
{
Query.Where(product => product.Id == id).AsTracking();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Ardalis.Specification.UnitTests.Fixture.Specs;

public class ProductByIdAsUntrackedWithIdentityResolutionSpec : Specification<Product>, ISingleResultSpecification
{
public ProductByIdAsUntrackedWithIdentityResolutionSpec(int id)
{
Query.Where(product => product.Id == id).AsNoTrackingWithIdentityResolution();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Ardalis.Specification.UnitTests.Fixture.Specs;

public class ProductByIdSpec : Specification<Product>
{
public ProductByIdSpec(int id)
{
Query.Where(x => x.Id == id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Ardalis.Specification.UnitTests.Fixture.Specs;

public class ProductByStoreIdSpec : Specification<Product>
{
public ProductByStoreIdSpec(int storeId)
{
Query.Where(x => x.StoreId == storeId);
}
}
1 change: 1 addition & 0 deletions sample/Ardalis.Sample.App3/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface IRepository<T> where T : class, IAggregateRoot
Task UpdateAsync(T entity, CancellationToken cancellationToken = default);
Task DeleteAsync(T entity, CancellationToken cancellationToken = default);
Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default);
Task DeleteRangeAsync(ISpecification<T> specification, CancellationToken cancellationToken = default);
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);

Task<T?> FindAsync<TId>(TId id, CancellationToken cancellationToken = default) where TId : notnull;
Expand Down
7 changes: 7 additions & 0 deletions sample/Ardalis.Sample.App3/RepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public virtual async Task DeleteRangeAsync(IEnumerable<T> entities, Cancellation
{
_dbContext.Set<T>().RemoveRange(entities);

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

await SaveChangesAsync(cancellationToken);
}
public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
Expand Down