Skip to content

Commit 935c14a

Browse files
committed
Remove Include filter and use autoinclude for EF Core
1 parent 8e58266 commit 935c14a

File tree

9 files changed

+8
-30
lines changed

9 files changed

+8
-30
lines changed

LinkDotNet.Blog.IntegrationTests/Infrastructure/Persistence/Sql/BlogPostRepositoryTests.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public async Task ShouldLoadBlogPost()
1717
await DbContext.BlogPosts.AddAsync(blogPost);
1818
await DbContext.SaveChangesAsync();
1919

20-
var blogPostFromRepo = await Repository.GetByIdAsync(blogPost.Id, post => post.Tags);
20+
var blogPostFromRepo = await Repository.GetByIdAsync(blogPost.Id);
2121

2222
blogPostFromRepo.Should().NotBeNull();
2323
blogPostFromRepo.Title.Should().Be("Title");
@@ -56,7 +56,7 @@ public async Task ShouldGetAllBlogPosts()
5656
await DbContext.BlogPosts.AddAsync(blogPost);
5757
await DbContext.SaveChangesAsync();
5858

59-
var blogPostsFromRepo = (await Repository.GetAllAsync(include: post => post.Tags)).ToList();
59+
var blogPostsFromRepo = (await Repository.GetAllAsync()).ToList();
6060

6161
blogPostsFromRepo.Should().NotBeNull();
6262
blogPostsFromRepo.Should().HaveCount(1);
@@ -100,7 +100,6 @@ public async Task ShouldFilterAndOrder()
100100
var blogPosts = await Repository.GetAllAsync(
101101
bp => bp.Title != "FilterOut",
102102
bp => bp.UpdatedDate,
103-
null,
104103
false);
105104

106105
var retrievedPosts = blogPosts.ToList();

LinkDotNet.Blog.IntegrationTests/Web/Shared/ProfileTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ private static void SetupGetAll(
153153
repoMock.Setup(r => r.GetAllAsync(
154154
It.IsAny<Expression<Func<ProfileInformationEntry, bool>>>(),
155155
It.IsAny<Expression<Func<ProfileInformationEntry, object>>>(),
156-
It.IsAny<Expression<Func<ProfileInformationEntry, object>>>(),
157156
It.IsAny<bool>(),
158157
It.IsAny<int>(),
159158
It.IsAny<int>()))
@@ -170,7 +169,6 @@ private static void SetupGetAll(
170169
repoMock.Setup(r => r.GetAllAsync(
171170
It.IsAny<Expression<Func<ProfileInformationEntry, bool>>>(),
172171
It.IsAny<Expression<Func<ProfileInformationEntry, object>>>(),
173-
It.IsAny<Expression<Func<ProfileInformationEntry, object>>>(),
174172
It.IsAny<bool>(),
175173
It.IsAny<int>(),
176174
It.IsAny<int>())).ReturnsAsync(new List<ProfileInformationEntry>().ToPagedList());

LinkDotNet.Blog.UnitTests/Infrastructure/Persistence/InMemory/BlogPostRepositoryTests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public async Task ShouldFilterAndOrder()
5555
var blogPosts = await sut.GetAllAsync(
5656
bp => bp.Title != "FilterOut",
5757
bp => bp.UpdatedDate,
58-
null,
5958
false);
6059

6160
var retrievedPosts = blogPosts.ToList();

LinkDotNet.Blog.Web/Pages/Index.razor

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
currentPage = await blogPostRepository.GetAllAsync(
3838
p => p.IsPublished,
3939
b => b.UpdatedDate,
40-
b => b.Tags,
4140
pageSize: appConfiguration.BlogPostsPerPage);
4241
}
4342

LinkDotNet.Infrastructure/Persistence/IRepository.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ namespace LinkDotNet.Infrastructure.Persistence
99
public interface IRepository<TEntity>
1010
where TEntity : Entity
1111
{
12-
Task<TEntity> GetByIdAsync(string id, Expression<Func<TEntity, object>> include = null);
12+
Task<TEntity> GetByIdAsync(string id);
1313

1414
Task<IPagedList<TEntity>> GetAllAsync(
1515
Expression<Func<TEntity, bool>> filter = null,
1616
Expression<Func<TEntity,
1717
object>> orderBy = null,
18-
Expression<Func<TEntity, object>> include = null,
1918
bool descending = true,
2019
int page = 1,
2120
int pageSize = int.MaxValue);

LinkDotNet.Infrastructure/Persistence/InMemory/Repository.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class Repository<TEntity> : IRepository<TEntity>
1313
{
1414
private readonly List<TEntity> entities = new();
1515

16-
public Task<TEntity> GetByIdAsync(string id, Expression<Func<TEntity, object>> include = null)
16+
public Task<TEntity> GetByIdAsync(string id)
1717
{
1818
var entity = entities.SingleOrDefault(b => b.Id == id);
1919
return Task.FromResult(entity);
@@ -22,7 +22,6 @@ public Task<TEntity> GetByIdAsync(string id, Expression<Func<TEntity, object>> i
2222
public Task<IPagedList<TEntity>> GetAllAsync(
2323
Expression<Func<TEntity, bool>> filter = null,
2424
Expression<Func<TEntity, object>> orderBy = null,
25-
Expression<Func<TEntity, object>> include = null,
2625
bool descending = true,
2726
int page = 1,
2827
int pageSize = int.MaxValue)

LinkDotNet.Infrastructure/Persistence/RavenDb/Repository.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public Repository(IDocumentStore documentStore)
1818
this.documentStore = documentStore;
1919
}
2020

21-
public async Task<TEntity> GetByIdAsync(string id, Expression<Func<TEntity, object>> include = null)
21+
public async Task<TEntity> GetByIdAsync(string id)
2222
{
2323
using var session = documentStore.OpenAsyncSession();
2424
return await session.LoadAsync<TEntity>(id);
@@ -27,7 +27,6 @@ public async Task<TEntity> GetByIdAsync(string id, Expression<Func<TEntity, obje
2727
public async Task<IPagedList<TEntity>> GetAllAsync(
2828
Expression<Func<TEntity, bool>> filter = null,
2929
Expression<Func<TEntity, object>> orderBy = null,
30-
Expression<Func<TEntity, object>> include = null,
3130
bool descending = true,
3231
int page = 1,
3332
int pageSize = int.MaxValue)

LinkDotNet.Infrastructure/Persistence/Sql/Mapping/BlogPostConfiguration.cs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public void Configure(EntityTypeBuilder<BlogPost> builder)
1414
builder.HasMany(t => t.Tags)
1515
.WithOne()
1616
.OnDelete(DeleteBehavior.Cascade);
17+
builder.Navigation(x => x.Tags).AutoInclude();
1718
}
1819
}
1920
}

LinkDotNet.Infrastructure/Persistence/Sql/Repository.cs

+2-17
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,19 @@ public Repository(BlogDbContext blogDbContext)
1818
this.blogDbContext = blogDbContext;
1919
}
2020

21-
public async Task<TEntity> GetByIdAsync(string id, Expression<Func<TEntity, object>> include = null)
21+
public async Task<TEntity> GetByIdAsync(string id)
2222
{
23-
if (include != null)
24-
{
25-
return await blogDbContext.Set<TEntity>().Include(include).SingleOrDefaultAsync(b => b.Id == id);
26-
}
27-
2823
return await blogDbContext.Set<TEntity>().SingleOrDefaultAsync(b => b.Id == id);
2924
}
3025

3126
public async Task<IPagedList<TEntity>> GetAllAsync(
3227
Expression<Func<TEntity, bool>> filter = null,
3328
Expression<Func<TEntity, object>> orderBy = null,
34-
Expression<Func<TEntity, object>> include = null,
3529
bool descending = true,
3630
int page = 1,
3731
int pageSize = int.MaxValue)
3832
{
39-
IQueryable<TEntity> entity;
40-
41-
if (include != null)
42-
{
43-
entity = blogDbContext.Set<TEntity>().AsNoTracking().Include(include).AsQueryable();
44-
}
45-
else
46-
{
47-
entity = blogDbContext.Set<TEntity>().AsNoTracking().AsQueryable();
48-
}
33+
var entity = blogDbContext.Set<TEntity>().AsNoTracking().AsQueryable();
4934

5035
if (filter != null)
5136
{

0 commit comments

Comments
 (0)