-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added AsTracking feature. * Accounted for tracking order. * Added tests.
- Loading branch information
Showing
12 changed files
with
177 additions
and
4 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...eworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsTrackingEvaluator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Ardalis.Specification.EntityFrameworkCore | ||
{ | ||
public class AsTrackingEvaluator : IEvaluator | ||
{ | ||
private AsTrackingEvaluator() { } | ||
public static AsTrackingEvaluator Instance { get; } = new AsTrackingEvaluator(); | ||
|
||
public bool IsCriteriaEvaluator { get; } = true; | ||
|
||
public IQueryable<T> GetQuery<T>(IQueryable<T> query, ISpecification<T> specification) where T : class | ||
{ | ||
if (specification.AsTracking) | ||
{ | ||
query = query.AsTracking(); | ||
} | ||
|
||
return query; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsTracking.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Ardalis.Specification.UnitTests.Fixture.Specs; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Ardalis.Specification.UnitTests.BuilderTests | ||
{ | ||
public class SpecificationBuilderExtensions_AsTracking | ||
{ | ||
[Fact] | ||
public void DoesNothing_GivenSpecWithoutAsTracking() | ||
{ | ||
var spec = new StoreEmptySpec(); | ||
|
||
spec.AsTracking.Should().Be(false); | ||
} | ||
|
||
[Fact] | ||
public void DoesNothing_GivenAsTrackingWithFalseCondition() | ||
{ | ||
var spec = new CompanyByIdWithFalseConditions(1); | ||
|
||
spec.AsTracking.Should().Be(false); | ||
} | ||
|
||
[Fact] | ||
public void FlagsAsTracking_GivenSpecWithAsTracking() | ||
{ | ||
var spec = new CompanyByIdAsTrackedSpec(1); | ||
|
||
spec.AsTracking.Should().Be(true); | ||
} | ||
|
||
[Fact] | ||
public void FlagsAsTracking_GivenSpecWithAsNoTrackingAndEndWithAsTracking() | ||
{ | ||
var spec = new CompanyByIdWithAsNoTrackingAsTrackedSpec(1); | ||
|
||
spec.AsTracking.Should().Be(true); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsTrackedSpec.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Ardalis.Specification.UnitTests.Fixture.Entities; | ||
|
||
namespace Ardalis.Specification.UnitTests.Fixture.Specs | ||
{ | ||
public class CompanyByIdAsTrackedSpec : Specification<Company>, ISingleResultSpecification<Company> | ||
{ | ||
public CompanyByIdAsTrackedSpec(int id) | ||
{ | ||
Query.Where(company => company.Id == id).AsTracking(); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsNoTrackingAsTrackedSpec.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Ardalis.Specification.UnitTests.Fixture.Entities; | ||
|
||
namespace Ardalis.Specification.UnitTests.Fixture.Specs | ||
{ | ||
public class CompanyByIdWithAsNoTrackingAsTrackedSpec : Specification<Company>, ISingleResultSpecification<Company> | ||
{ | ||
public CompanyByIdWithAsNoTrackingAsTrackedSpec(int id) | ||
{ | ||
Query.Where(company => company.Id == id).AsNoTracking().AsNoTrackingWithIdentityResolution().AsTracking(); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsTrackingAsUntrackedSpec.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Ardalis.Specification.UnitTests.Fixture.Entities; | ||
|
||
namespace Ardalis.Specification.UnitTests.Fixture.Specs | ||
{ | ||
public class CompanyByIdWithAsTrackingAsUntrackedSpec : Specification<Company>, ISingleResultSpecification<Company> | ||
{ | ||
public CompanyByIdWithAsTrackingAsUntrackedSpec(int id) | ||
{ | ||
Query.Where(company => company.Id == id).AsTracking().AsNoTracking(); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...UnitTests/Fixture/Specs/CompanyByIdWithAsTrackingAsUntrackedWithIdentityResolutionSpec.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Ardalis.Specification.UnitTests.Fixture.Entities; | ||
|
||
namespace Ardalis.Specification.UnitTests.Fixture.Specs | ||
{ | ||
public class CompanyByIdWithAsTrackingAsUntrackedWithIdentityResolutionSpec : Specification<Company>, | ||
ISingleResultSpecification<Company> | ||
{ | ||
public CompanyByIdWithAsTrackingAsUntrackedWithIdentityResolutionSpec(int id) | ||
{ | ||
Query.Where(company => company.Id == id).AsTracking().AsNoTrackingWithIdentityResolution(); | ||
} | ||
} | ||
} |