Skip to content

Commit d30a4b9

Browse files
author
coolbyte
committed
Change the property types of ISpecification<T>.AsNoTracking and ISpecification<T>.AsNoTrackingWithIdentityResolution back to bool, add a property ISpecification<T>.TrackingFlag, so this is not a breaking change. Change AsNoTrackingEvaluator, AsNoTrackingWithIdentityResolutionEvaluator and AsTrackingEvaluator to check the ISpecification<T>.TrackingFlag value. If AsTracking, AsNoTracking, and AsNoTrackingWithIdentityResolution are called at the same time, only the last call will be active, If none of AsTracking, AsNoTracking, and AsNoTrackingWithIdentityResolution is called, the consumer's default tracking behavior will be used(default tracking behavior could be configured by using DbContextOptionsBuilder.UseQueryTrackingBehavior method).
1 parent b296a7a commit d30a4b9

File tree

9 files changed

+38
-15
lines changed

9 files changed

+38
-15
lines changed

Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsNoTrackingEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private AsNoTrackingEvaluator() { }
1515

1616
public IQueryable<T> GetQuery<T>(IQueryable<T> query, ISpecification<T> specification) where T : class
1717
{
18-
if (specification.AsNoTracking == true)
18+
if (specification is { TrackingFlag: true, AsNoTracking: true })
1919
{
2020
query = query.AsNoTracking();
2121
}

Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsNoTrackingWithIdentityResolutionEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ private AsNoTrackingWithIdentityResolutionEvaluator() { }
1616

1717
public IQueryable<T> GetQuery<T>(IQueryable<T> query, ISpecification<T> specification) where T : class
1818
{
19-
if (specification.AsNoTrackingWithIdentityResolution == true)
19+
if (specification is { TrackingFlag: true, AsNoTrackingWithIdentityResolution: true })
2020
{
2121
query = query.AsNoTrackingWithIdentityResolution();
2222
}

Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsTrackingEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ private AsTrackingEvaluator() { }
1616

1717
/// <inheritdoc />
1818
public IQueryable<T> GetQuery<T>(IQueryable<T> query, ISpecification<T> specification) where T : class =>
19-
specification is { AsNoTracking: false, AsNoTrackingWithIdentityResolution: false }
19+
specification is { TrackingFlag: true, AsNoTracking: false, AsNoTrackingWithIdentityResolution: false }
2020
? query.AsTracking()
2121
: query;
2222
}

Specification/src/Ardalis.Specification/Builder/SpecificationBuilderExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ public static ISpecificationBuilder<T> AsNoTracking<T>(
392392
if (condition)
393393
{
394394
specificationBuilder.Specification.AsNoTracking = true;
395+
specificationBuilder.Specification.TrackingFlag = true;
395396
}
396397

397398
return specificationBuilder;
@@ -428,6 +429,7 @@ public static ISpecificationBuilder<T> AsTracking<T>(
428429
{
429430
specificationBuilder.Specification.AsNoTracking = false;
430431
specificationBuilder.Specification.AsNoTrackingWithIdentityResolution = false;
432+
specificationBuilder.Specification.TrackingFlag = true;
431433
}
432434

433435
return specificationBuilder;
@@ -500,6 +502,7 @@ public static ISpecificationBuilder<T> AsNoTrackingWithIdentityResolution<T>(
500502
if (condition)
501503
{
502504
specificationBuilder.Specification.AsNoTrackingWithIdentityResolution = true;
505+
specificationBuilder.Specification.TrackingFlag = true;
503506
}
504507

505508
return specificationBuilder;

Specification/src/Ardalis.Specification/ISpecification.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public interface ISpecification<T>
102102
/// that are returned. When true, if the entity instances are modified, this will not be detected
103103
/// by the change tracker.
104104
/// </summary>
105-
bool? AsNoTracking { get; }
105+
bool AsNoTracking { get; }
106106

107107
/// <summary>
108108
/// Returns whether or not the generated sql query should be split into multiple SQL queries
@@ -121,7 +121,7 @@ public interface ISpecification<T>
121121
/// <remarks>
122122
/// for more info: https://docs.microsoft.com/en-us/ef/core/change-tracking/identity-resolution#identity-resolution-and-queries
123123
/// </remarks>
124-
bool? AsNoTrackingWithIdentityResolution { get; }
124+
bool AsNoTrackingWithIdentityResolution { get; }
125125

126126
/// <summary>
127127
/// Returns whether or not the query should ignore the defined global query filters
@@ -130,6 +130,11 @@ public interface ISpecification<T>
130130
/// for more info: https://docs.microsoft.com/en-us/ef/core/querying/filters
131131
/// </remarks>
132132
bool IgnoreQueryFilters { get; }
133+
134+
/// <summary>
135+
/// Returns true when tracking behavior is changed
136+
/// </summary>
137+
bool TrackingFlag { get; }
133138

134139
/// <summary>
135140
/// Applies the query defined within the specification to the given objects.

Specification/src/Ardalis.Specification/Specification.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,18 @@ public virtual bool IsSatisfiedBy(T entity)
109109
public bool CacheEnabled { get; internal set; }
110110

111111
/// <inheritdoc/>
112-
public bool? AsNoTracking { get; internal set; }
112+
public bool AsNoTracking { get; internal set; }
113113

114114
/// <inheritdoc/>
115115
public bool AsSplitQuery { get; internal set; } = false;
116116

117117
/// <inheritdoc/>
118-
public bool? AsNoTrackingWithIdentityResolution { get; internal set; }
118+
public bool AsNoTrackingWithIdentityResolution { get; internal set; }
119119

120120
/// <inheritdoc/>
121121
public bool IgnoreQueryFilters { get; internal set; } = false;
122+
123+
/// <inheritdoc/>
124+
public bool TrackingFlag { get; internal set;}
122125
}
123126
}

Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsNoTracking.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ public void DoesNothing_GivenSpecWithoutAsNoTracking()
1111
{
1212
var spec = new StoreEmptySpec();
1313

14-
spec.AsNoTracking.Should().Be(null);
14+
spec.AsNoTracking.Should().Be(false);
15+
spec.TrackingFlag.Should().Be(false);
1516
}
1617

1718
[Fact]
1819
public void DoesNothing_GivenAsNoTrackingWithFalseCondition()
1920
{
2021
var spec = new CompanyByIdWithFalseConditions(1);
2122

22-
spec.AsNoTracking.Should().Be(null);
23+
spec.AsNoTracking.Should().Be(false);
24+
spec.TrackingFlag.Should().Be(false);
2325
}
2426

2527
[Fact]
@@ -28,6 +30,7 @@ public void FlagsAsNoTracking_GivenSpecWithAsNoTracking()
2830
var spec = new CompanyByIdAsUntrackedSpec(1);
2931

3032
spec.AsNoTracking.Should().Be(true);
33+
spec.TrackingFlag.Should().Be(true);
3134
}
3235

3336
[Fact]
@@ -36,6 +39,7 @@ void FlagsAsNoTracking_GivenSpecWithAsTrackingAndEndWithAsNoTracking()
3639
var spec = new CompanyByIdWithAsTrackingAsUntrackedSpec(1);
3740

3841
spec.AsNoTracking.Should().Be(true);
42+
spec.TrackingFlag.Should().Be(true);
3943
}
4044
}
4145
}

Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsNoTrackingWithIdentityResolution.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ public void DoesNothing_GivenSpecWithoutAsNoTrackingWithIdentityResolution()
1111
{
1212
var spec = new StoreEmptySpec();
1313

14-
spec.AsNoTrackingWithIdentityResolution.Should().Be(null);
14+
spec.AsNoTrackingWithIdentityResolution.Should().Be(false);
15+
spec.TrackingFlag.Should().Be(false);
1516
}
1617

1718
[Fact]
1819
public void DoesNothing_GivenAsNoTrackingWithIdentityResolutionWithFalseCondition()
1920
{
2021
var spec = new CompanyByIdWithFalseConditions(1);
2122

22-
spec.AsNoTrackingWithIdentityResolution.Should().Be(null);
23+
spec.AsNoTrackingWithIdentityResolution.Should().Be(false);
24+
spec.TrackingFlag.Should().Be(false);
2325
}
2426

2527
[Fact]
@@ -28,6 +30,7 @@ public void FlagsAsNoTracking_GivenSpecWithAsNoTrackingWithIdentityResolution()
2830
var spec = new CompanyByIdAsUntrackedWithIdentityResolutionSpec(1);
2931

3032
spec.AsNoTrackingWithIdentityResolution.Should().Be(true);
33+
spec.TrackingFlag.Should().Be(true);
3134
}
3235

3336
[Fact]
@@ -36,6 +39,7 @@ void FlagsAsNoTracking_GivenSpecWithAsTrackingAndEndWithAsNoTrackingWithIdentity
3639
var spec = new CompanyByIdWithAsTrackingAsUntrackedWithIdentityResolutionSpec(1);
3740

3841
spec.AsNoTrackingWithIdentityResolution.Should().Be(true);
42+
spec.TrackingFlag.Should().Be(true);
3943
}
4044
}
4145
}

Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsTracking.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@ public void DoesNothing_GivenSpecWithoutAsTracking()
1111
{
1212
var spec = new StoreEmptySpec();
1313

14-
spec.AsNoTracking.Should().BeNull();
15-
spec.AsNoTrackingWithIdentityResolution.Should().BeNull();
14+
spec.AsNoTracking.Should().Be(false);
15+
spec.AsNoTrackingWithIdentityResolution.Should().Be(false);
16+
spec.TrackingFlag.Should().Be(false);
1617
}
1718

1819
[Fact]
1920
public void DoesNothing_GivenAsTrackingWithFalseCondition()
2021
{
2122
var spec = new CompanyByIdWithFalseConditions(1);
2223

23-
spec.AsNoTracking.Should().BeNull();
24-
spec.AsNoTrackingWithIdentityResolution.Should().BeNull();
24+
spec.AsNoTracking.Should().Be(false);
25+
spec.AsNoTrackingWithIdentityResolution.Should().Be(false);
26+
spec.TrackingFlag.Should().Be(false);
2527
}
2628

2729
[Fact]
@@ -31,6 +33,7 @@ public void FlagsAsTracking_GivenSpecWithAsTracking()
3133

3234
spec.AsNoTracking.Should().Be(false);
3335
spec.AsNoTrackingWithIdentityResolution.Should().Be(false);
36+
spec.TrackingFlag.Should().Be(true);
3437
}
3538

3639
[Fact]
@@ -40,6 +43,7 @@ public void FlagsAsTracking_GivenSpecWithAsNoTrackingAndEndWithAsTracking()
4043

4144
spec.AsNoTracking.Should().Be(false);
4245
spec.AsNoTrackingWithIdentityResolution.Should().Be(false);
46+
spec.TrackingFlag.Should().Be(true);
4347
}
4448
}
4549
}

0 commit comments

Comments
 (0)