Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public async Task ReturnsStoreContainingCity1_GivenStoreIncludeProductsSpec()

result.Should().NotBeNull();
result.Should().ContainSingle();
result[0].Id.Should().Be(StoreSeed.VALID_Search_City_ID);
result[0].Id.Should().Be(StoreSeed.VALID_Search_ID);
result[0].City.Should().Contain(StoreSeed.VALID_Search_City_Key);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public virtual async Task ReturnsStoreContainingCity1_GivenStoreIncludeProductsS

result.Should().NotBeNull();
result.Should().ContainSingle();
result[0].Id.Should().Be(StoreSeed.VALID_Search_City_ID);
result[0].Id.Should().Be(StoreSeed.VALID_Search_ID);
result[0].City.Should().Contain(StoreSeed.VALID_Search_City_Key);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
public class CacheSpecificationBuilder<T> : ICacheSpecificationBuilder<T> where T : class
{
public Specification<T> Specification { get; }
public bool IsChainDiscarded { get; set; }

public CacheSpecificationBuilder(Specification<T> specification)
:this(specification, false)
{
Specification = specification;
}

public CacheSpecificationBuilder(Specification<T> specification, bool isChainDiscarded)
{
this.Specification = specification;
this.IsChainDiscarded = isChainDiscarded;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
{
public interface ICacheSpecificationBuilder<T> : ISpecificationBuilder<T> where T : class
{
bool IsChainDiscarded { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ namespace Ardalis.Specification
{
public interface IIncludableSpecificationBuilder<T, out TProperty> : ISpecificationBuilder<T> where T : class
{
bool IsChainDiscarded { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ namespace Ardalis.Specification
{
public interface IOrderedSpecificationBuilder<T> : ISpecificationBuilder<T>
{
bool IsChainDiscarded { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@ public static IIncludableSpecificationBuilder<TEntity, TProperty> ThenInclude<TE
this IIncludableSpecificationBuilder<TEntity, TPreviousProperty> previousBuilder,
Expression<Func<TPreviousProperty, TProperty>> thenIncludeExpression)
where TEntity : class
=> ThenInclude(previousBuilder, thenIncludeExpression, true);

public static IIncludableSpecificationBuilder<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(
this IIncludableSpecificationBuilder<TEntity, TPreviousProperty> previousBuilder,
Expression<Func<TPreviousProperty, TProperty>> thenIncludeExpression,
bool condition)
where TEntity : class
{
var info = new IncludeExpressionInfo(thenIncludeExpression, typeof(TEntity), typeof(TProperty), typeof(TPreviousProperty));
if (condition && !previousBuilder.IsChainDiscarded)
{
var info = new IncludeExpressionInfo(thenIncludeExpression, typeof(TEntity), typeof(TProperty), typeof(TPreviousProperty));

((List<IncludeExpressionInfo>)previousBuilder.Specification.IncludeExpressions).Add(info);
((List<IncludeExpressionInfo>)previousBuilder.Specification.IncludeExpressions).Add(info);
}

var includeBuilder = new IncludableSpecificationBuilder<TEntity, TProperty>(previousBuilder.Specification);
var includeBuilder = new IncludableSpecificationBuilder<TEntity, TProperty>(previousBuilder.Specification, !condition || previousBuilder.IsChainDiscarded);

return includeBuilder;
}
Expand All @@ -24,12 +34,22 @@ public static IIncludableSpecificationBuilder<TEntity, TProperty> ThenInclude<TE
this IIncludableSpecificationBuilder<TEntity, IEnumerable<TPreviousProperty>> previousBuilder,
Expression<Func<TPreviousProperty, TProperty>> thenIncludeExpression)
where TEntity : class
=> ThenInclude(previousBuilder, thenIncludeExpression, true);

public static IIncludableSpecificationBuilder<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(
this IIncludableSpecificationBuilder<TEntity, IEnumerable<TPreviousProperty>> previousBuilder,
Expression<Func<TPreviousProperty, TProperty>> thenIncludeExpression,
bool condition)
where TEntity : class
{
var info = new IncludeExpressionInfo(thenIncludeExpression, typeof(TEntity), typeof(TProperty), typeof(IEnumerable<TPreviousProperty>));
if (condition && !previousBuilder.IsChainDiscarded)
{
var info = new IncludeExpressionInfo(thenIncludeExpression, typeof(TEntity), typeof(TProperty), typeof(IEnumerable<TPreviousProperty>));

((List<IncludeExpressionInfo>)previousBuilder.Specification.IncludeExpressions).Add(info);
((List<IncludeExpressionInfo>)previousBuilder.Specification.IncludeExpressions).Add(info);
}

var includeBuilder = new IncludableSpecificationBuilder<TEntity, TProperty>(previousBuilder.Specification);
var includeBuilder = new IncludableSpecificationBuilder<TEntity, TProperty>(previousBuilder.Specification, !condition || previousBuilder.IsChainDiscarded);

return includeBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ namespace Ardalis.Specification
public class IncludableSpecificationBuilder<T, TProperty> : IIncludableSpecificationBuilder<T, TProperty> where T : class
{
public Specification<T> Specification { get; }
public bool IsChainDiscarded { get; set; }

public IncludableSpecificationBuilder(Specification<T> specification)
:this(specification, false)
{
Specification = specification;
}

public IncludableSpecificationBuilder(Specification<T> specification, bool isChainDiscarded)
{
this.Specification = specification;
this.IsChainDiscarded = isChainDiscarded;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Ardalis.Specification
Expand All @@ -9,17 +10,43 @@ public static class OrderedBuilderExtensions
public static IOrderedSpecificationBuilder<T> ThenBy<T>(
this IOrderedSpecificationBuilder<T> orderedBuilder,
Expression<Func<T, object?>> orderExpression)
=> ThenBy(orderedBuilder, orderExpression, true);

public static IOrderedSpecificationBuilder<T> ThenBy<T>(
this IOrderedSpecificationBuilder<T> orderedBuilder,
Expression<Func<T, object?>> orderExpression,
bool condition)
{
((List<OrderExpressionInfo<T>>)orderedBuilder.Specification.OrderExpressions).Add(new OrderExpressionInfo<T>(orderExpression, OrderTypeEnum.ThenBy));
if (condition && !orderedBuilder.IsChainDiscarded)
{
((List<OrderExpressionInfo<T>>)orderedBuilder.Specification.OrderExpressions).Add(new OrderExpressionInfo<T>(orderExpression, OrderTypeEnum.ThenBy));
}
else
{
orderedBuilder.IsChainDiscarded = true;
}

return orderedBuilder;
}

public static IOrderedSpecificationBuilder<T> ThenByDescending<T>(
this IOrderedSpecificationBuilder<T> orderedBuilder,
Expression<Func<T, object?>> orderExpression)
=> ThenByDescending(orderedBuilder, orderExpression, true);

public static IOrderedSpecificationBuilder<T> ThenByDescending<T>(
this IOrderedSpecificationBuilder<T> orderedBuilder,
Expression<Func<T, object?>> orderExpression,
bool condition)
{
((List<OrderExpressionInfo<T>>)orderedBuilder.Specification.OrderExpressions).Add(new OrderExpressionInfo<T>(orderExpression, OrderTypeEnum.ThenByDescending));
if (condition && !orderedBuilder.IsChainDiscarded)
{
((List<OrderExpressionInfo<T>>)orderedBuilder.Specification.OrderExpressions).Add(new OrderExpressionInfo<T>(orderExpression, OrderTypeEnum.ThenByDescending));
}
else
{
orderedBuilder.IsChainDiscarded = true;
}

return orderedBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ namespace Ardalis.Specification
public class OrderedSpecificationBuilder<T> : IOrderedSpecificationBuilder<T>
{
public Specification<T> Specification { get; }
public bool IsChainDiscarded { get; set; }

public OrderedSpecificationBuilder(Specification<T> specification)
:this(specification, false)
{
this.Specification = specification;
}


public OrderedSpecificationBuilder(Specification<T> specification, bool isChainDiscarded)
{
this.Specification = specification;
this.IsChainDiscarded = isChainDiscarded;
}
}
}
Loading