Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "AsNoTracking" feature. (#71) #72

Merged
merged 1 commit into from
Jan 24, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,12 @@ public static class SpecificationBuilderExtensions

return specificationBuilder;
}

public static ISpecificationBuilder<T> AsNoTracking<T>(
this ISpecificationBuilder<T> specificationBuilder)
{
specificationBuilder.Specification.AsNoTracking = true;
return specificationBuilder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ public interface ISpecification<T>

bool CacheEnabled { get; }
string? CacheKey { get; }

bool AsNoTracking { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ protected Specification()
public string? CacheKey { get; internal set; }

public bool CacheEnabled { get; internal set; }

public bool AsNoTracking { get; internal set; } = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public virtual IQueryable<T> GetQuery(IQueryable<T> inputQuery, ISpecification<T
query = query.Take(specification.Take.Value);
}

if (specification.AsNoTracking)
{
query = query.AsNoTracking();
}

return query;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,21 @@ public async Task GetCompanyUsingRepositoryAnd_CompanyWithStoresThenIncludeProdu
result.Stores.Count.Should().BeGreaterThan(49);
result.Stores.Select(x => x.Products).Count().Should().BeGreaterThan(1);
}

[Fact]
public async Task GetCompanyUsingRepositoryAnd_CompanyAsUntrackedSpec_ShouldNotBeTracked()
{
var result = (await companyRepository.ListAsync(new CompanySpec(CompanySeed.VALID_COMPANY_ID))).SingleOrDefault();
dbContext.Entry(result).State = Microsoft.EntityFrameworkCore.EntityState.Detached;

result.Should().NotBeNull();
result.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME);

result = (await companyRepository.ListAsync(new CompanyAsUntrackedSpec(CompanySeed.VALID_COMPANY_ID))).SingleOrDefault();

result.Should().NotBeNull();
result.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME);
dbContext.Entry(result).State.Should().Be(Microsoft.EntityFrameworkCore.EntityState.Detached);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Data;
using System;
using System.Collections.Generic;
using System.Text;

namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Specs
{
public class CompanyAsUntrackedSpec : Specification<Company>
{
public CompanyAsUntrackedSpec(int id)
{
Query.Where(company => company.Id == id).AsNoTracking();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Data;
using System;
using System.Collections.Generic;
using System.Text;

namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Specs
{
public class CompanySpec : Specification<Company>
{
public CompanySpec(int id)
{
Query.Where(company => company.Id == id);
}
}
}