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: 2 additions & 0 deletions src/Ardalis.Specification/ISpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,6 @@ 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);
}
48 changes: 48 additions & 0 deletions src/Ardalis.Specification/Specification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,52 @@ public virtual bool IsSatisfiedBy(T entity)
var validator = Validator;
return validator.IsValid(entity, this);
}

void ISpecification<T>.CopyTo(Specification<T> otherSpec)
{
otherSpec.PostProcessingAction = PostProcessingAction;
otherSpec.QueryTag = QueryTag;
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 is not null)
{
otherSpec._whereExpressions = _whereExpressions.ToList();
}

if (_includeExpressions is not null)
{
otherSpec._includeExpressions = _includeExpressions.ToList();
}

if (_includeStrings is not null)
{
otherSpec._includeStrings = _includeStrings.ToList();
}

if (_orderExpressions is not null)
{
otherSpec._orderExpressions = _orderExpressions.ToList();
}

if (_searchExpressions is not null)
{
otherSpec._searchExpressions = _searchExpressions.ToList();
}

if (_items is not null)
{
otherSpec._items = new Dictionary<string, object>(_items);
}
}
}
14 changes: 14 additions & 0 deletions src/Ardalis.Specification/SpecificationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Ardalis.Specification;

public static class SpecificationExtensions
{
public static Specification<T, TResult> WithProjectionOf<T, TResult>(this ISpecification<T> source, ISpecification<T, TResult> projectionSpec)
{
var newSpec = new Specification<T, TResult>();
source.CopyTo(newSpec);
newSpec.Selector = projectionSpec.Selector;
newSpec.SelectorMany = projectionSpec.SelectorMany;
newSpec.PostProcessingAction = projectionSpec.PostProcessingAction;
return newSpec;
}
}
68 changes: 68 additions & 0 deletions tests/Ardalis.Specification.Tests/SpecificationExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
namespace Tests;

public class SpecificationExtensionsTests
{
private record Address(int Id, string Street);
private record Person(int Id, string Name, List<string> Names, Address Address);

[Fact]
public void WithProjectionOf_ReturnsCopyWithProjection()
{
var spec = new Specification<Person>();
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("testQuery")
.PostProcessingAction(x => x.Where(x => x.Id > 0));

var projectionSpec = new Specification<Person, string>();
projectionSpec.Query.Select(x => x.Name);
projectionSpec.Query.SelectMany(x => x.Names);
projectionSpec.Query.PostProcessingAction(x => x.Select(x => x + "A"));

var newSpec = spec.WithProjectionOf(projectionSpec);

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.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);
newSpec.QueryTag.Should().Be(spec.QueryTag);

newSpec.PostProcessingAction.Should().BeSameAs(projectionSpec.PostProcessingAction);
((Specification<Person>)newSpec).PostProcessingAction.Should().BeSameAs(spec.PostProcessingAction);
}
}