Skip to content
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
2 changes: 0 additions & 2 deletions src/Ardalis.Specification/ISpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,4 @@ public interface ISpecification<T>
/// <param name="entity">The entity to be validated</param>
/// <returns></returns>
bool IsSatisfiedBy(T entity);

internal void CopyTo(Specification<T> otherSpec);
}
76 changes: 31 additions & 45 deletions src/Ardalis.Specification/Specification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,55 +139,41 @@ public virtual bool IsSatisfiedBy(T entity)
return validator.IsValid(entity, this);
}

void ISpecification<T>.CopyTo(Specification<T> otherSpec)
internal Specification<T> Clone()
{
otherSpec.PostProcessingAction = PostProcessingAction;
otherSpec.CacheKey = CacheKey;
otherSpec.Take = Take;
otherSpec.Skip = Skip;
otherSpec.IgnoreQueryFilters = IgnoreQueryFilters;
otherSpec.IgnoreAutoIncludes = IgnoreAutoIncludes;
otherSpec.AsSplitQuery = AsSplitQuery;
otherSpec.AsNoTracking = AsNoTracking;
otherSpec.AsTracking = AsTracking;
otherSpec.AsNoTrackingWithIdentityResolution = AsNoTrackingWithIdentityResolution;

// The expression containers are immutable, having the same instance is fine.
// We'll just create new collections.

if (!_whereExpressions.IsEmpty)
{
otherSpec._whereExpressions = _whereExpressions.Clone();
}

if (!_includeExpressions.IsEmpty)
{
otherSpec._includeExpressions = _includeExpressions.Clone();
}

if (!_includeStrings.IsEmpty)
{
otherSpec._includeStrings = _includeStrings.Clone();
}

if (!_orderExpressions.IsEmpty)
{
otherSpec._orderExpressions = _orderExpressions.Clone();
}

if (!_searchExpressions.IsEmpty)
{
otherSpec._searchExpressions = _searchExpressions.Clone();
}
var newSpec = new Specification<T>();
CopyState(this, newSpec);
return newSpec;
}

if (!_queryTags.IsEmpty)
{
otherSpec._queryTags = _queryTags.Clone();
}
internal Specification<T, TResult> Clone<TResult>()
{
var newSpec = new Specification<T, TResult>();
CopyState(this, newSpec);
return newSpec;
}

if (_items is not null)
private static void CopyState(Specification<T> source, Specification<T> target)
{
target.PostProcessingAction = source.PostProcessingAction;
target.CacheKey = source.CacheKey;
target.Take = source.Take;
target.Skip = source.Skip;
target.IgnoreAutoIncludes = source.IgnoreAutoIncludes;
target.IgnoreQueryFilters = source.IgnoreQueryFilters;
target.AsSplitQuery = source.AsSplitQuery;
target.AsNoTracking = source.AsNoTracking;
target.AsTracking = source.AsTracking;
target.AsNoTrackingWithIdentityResolution = source.AsNoTrackingWithIdentityResolution;
target._whereExpressions = source._whereExpressions.Clone();
target._searchExpressions = source._searchExpressions.Clone();
target._orderExpressions = source._orderExpressions.Clone();
target._includeExpressions = source._includeExpressions.Clone();
target._includeStrings = source._includeStrings.Clone();
target._queryTags = source._queryTags.Clone();
if (source._items is not null)
{
otherSpec._items = new Dictionary<string, object>(_items);
target._items = new Dictionary<string, object>(source._items);
}
}
}
5 changes: 2 additions & 3 deletions src/Ardalis.Specification/SpecificationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ public static class SpecificationExtensions
/// <see langword="null"/>.</param>
/// <returns>A new <see cref="Specification{T, TResult}"/> that represents the result of applying the projection to the
/// source specification.</returns>
public static Specification<T, TResult> WithProjectionOf<T, TResult>(this ISpecification<T> source, ISpecification<T, TResult> projectionSpec)
public static Specification<T, TResult> WithProjectionOf<T, TResult>(this Specification<T> source, Specification<T, TResult> projectionSpec)
{
var newSpec = new Specification<T, TResult>();
source.CopyTo(newSpec);
var newSpec = source.Clone<TResult>();
newSpec.Selector = projectionSpec.Selector;
newSpec.SelectorMany = projectionSpec.SelectorMany;
newSpec.PostProcessingAction = projectionSpec.PostProcessingAction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,5 @@ public IEnumerable<T> Evaluate(IEnumerable<T> entities)
=> throw new NotImplementedException();
public bool IsSatisfiedBy(T entity)
=> throw new NotImplementedException();

void ISpecification<T>.CopyTo(Specification<T> otherSpec)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,5 @@ public IEnumerable<T> Evaluate(IEnumerable<T> entities)
=> throw new NotImplementedException();
public bool IsSatisfiedBy(T entity)
=> throw new NotImplementedException();

void ISpecification<T>.CopyTo(Specification<T> otherSpec)
{
throw new NotImplementedException();
}
}
}
114 changes: 114 additions & 0 deletions tests/Ardalis.Specification.Tests/SpecificationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,120 @@ public void CollectionsProperties_ReturnEmptyEnumerable_GivenEmptySpec()
spec.IncludeStrings.Should().BeSameAs(Enumerable.Empty<string>());
}

[Fact]
public void Clone_ReturnsCopy()
{
var spec = new Specification<Customer>();
spec.Items.Add("test", "test");
spec.Query
.Where(x => x.Name == "test")
.Include(x => x.Address)
.Include("Address")
.OrderBy(x => x.Id)
.Search(x => x.Name, "test")
.Take(2)
.Skip(3)
.WithCacheKey("testKey")
.IgnoreQueryFilters()
.IgnoreQueryFilters()
.AsSplitQuery()
.AsNoTracking()
.TagWith("testQuery1")
.PostProcessingAction(x => x.Where(x => x.Id > 0));

var newSpec = spec.Clone();

newSpec.Should().BeOfType<Specification<Customer>>();

newSpec.Items.Should().NotBeSameAs(spec.Items);
newSpec.Items.Should().BeEquivalentTo(spec.Items);

newSpec.WhereExpressions.Should().NotBeSameAs(spec.WhereExpressions);
newSpec.WhereExpressions.Should().Equal(spec.WhereExpressions);

newSpec.IncludeExpressions.Should().NotBeSameAs(spec.IncludeExpressions);
newSpec.IncludeExpressions.Should().Equal(spec.IncludeExpressions);

newSpec.IncludeStrings.Should().NotBeSameAs(spec.IncludeStrings);
newSpec.IncludeStrings.Should().Equal(spec.IncludeStrings);

newSpec.OrderExpressions.Should().NotBeSameAs(spec.OrderExpressions);
newSpec.OrderExpressions.Should().Equal(spec.OrderExpressions);

newSpec.SearchCriterias.Should().NotBeSameAs(spec.SearchCriterias);
newSpec.SearchCriterias.Should().Equal(spec.SearchCriterias);

newSpec.QueryTags.Should().NotBeSameAs(spec.QueryTags);
newSpec.QueryTags.Should().Equal(spec.QueryTags);

newSpec.Take.Should().Be(spec.Take);
newSpec.Skip.Should().Be(spec.Skip);
newSpec.CacheKey.Should().Be(spec.CacheKey);
newSpec.IgnoreQueryFilters.Should().Be(spec.IgnoreQueryFilters);
newSpec.IgnoreAutoIncludes.Should().Be(spec.IgnoreAutoIncludes);
newSpec.AsSplitQuery.Should().Be(spec.AsSplitQuery);
newSpec.AsNoTracking.Should().Be(spec.AsNoTracking);
newSpec.AsNoTrackingWithIdentityResolution.Should().Be(spec.AsNoTrackingWithIdentityResolution);
newSpec.AsTracking.Should().Be(spec.AsTracking);
}

[Fact]
public void Clone_ReturnsCopyWithProjectionType()
{
var spec = new Specification<Customer>();
spec.Items.Add("test", "test");
spec.Query
.Where(x => x.Name == "test")
.Include(x => x.Address)
.Include("Address")
.OrderBy(x => x.Id)
.Search(x => x.Name, "test")
.Take(2)
.Skip(3)
.WithCacheKey("testKey")
.IgnoreQueryFilters()
.IgnoreQueryFilters()
.AsSplitQuery()
.AsNoTracking()
.TagWith("testQuery1")
.PostProcessingAction(x => x.Where(x => x.Id > 0));

var newSpec = spec.Clone<string>();

newSpec.Should().BeOfType<Specification<Customer, string>>();

newSpec.Items.Should().NotBeSameAs(spec.Items);
newSpec.Items.Should().BeEquivalentTo(spec.Items);

newSpec.WhereExpressions.Should().NotBeSameAs(spec.WhereExpressions);
newSpec.WhereExpressions.Should().Equal(spec.WhereExpressions);

newSpec.IncludeExpressions.Should().NotBeSameAs(spec.IncludeExpressions);
newSpec.IncludeExpressions.Should().Equal(spec.IncludeExpressions);

newSpec.IncludeStrings.Should().NotBeSameAs(spec.IncludeStrings);
newSpec.IncludeStrings.Should().Equal(spec.IncludeStrings);

newSpec.OrderExpressions.Should().NotBeSameAs(spec.OrderExpressions);
newSpec.OrderExpressions.Should().Equal(spec.OrderExpressions);

newSpec.SearchCriterias.Should().NotBeSameAs(spec.SearchCriterias);
newSpec.SearchCriterias.Should().Equal(spec.SearchCriterias);

newSpec.QueryTags.Should().NotBeSameAs(spec.QueryTags);
newSpec.QueryTags.Should().Equal(spec.QueryTags);

newSpec.Take.Should().Be(spec.Take);
newSpec.Skip.Should().Be(spec.Skip);
newSpec.CacheKey.Should().Be(spec.CacheKey);
newSpec.IgnoreQueryFilters.Should().Be(spec.IgnoreQueryFilters);
newSpec.IgnoreAutoIncludes.Should().Be(spec.IgnoreAutoIncludes);
newSpec.AsSplitQuery.Should().Be(spec.AsSplitQuery);
newSpec.AsNoTracking.Should().Be(spec.AsNoTracking);
newSpec.AsNoTrackingWithIdentityResolution.Should().Be(spec.AsNoTrackingWithIdentityResolution);
newSpec.AsTracking.Should().Be(spec.AsTracking);
}

#if NET8_0_OR_GREATER
[Fact]
public void Items_InitializesOnFirstAccess()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,5 @@ public IEnumerable<T> Evaluate(IEnumerable<T> entities)
=> throw new NotImplementedException();
public bool IsSatisfiedBy(T entity)
=> throw new NotImplementedException();

void ISpecification<T>.CopyTo(Specification<T> otherSpec)
{
throw new NotImplementedException();
}
}
}