diff --git a/NetCoreEntityFramework.Tests/Mocks/TestEntityRepository.cs b/NetCoreEntityFramework.Tests/Mocks/TestEntityRepository.cs index 5246a27..3d6ca50 100644 --- a/NetCoreEntityFramework.Tests/Mocks/TestEntityRepository.cs +++ b/NetCoreEntityFramework.Tests/Mocks/TestEntityRepository.cs @@ -4,7 +4,7 @@ namespace Nodes.NetCore.EntityFramework.Tests.Mocks { public class TestEntityRepository : EntityRepository { - public TestEntityRepository(TestContext context) : base(context, context.Table) + public TestEntityRepository(TestContext context) : base(context) { } } diff --git a/NetCoreEntityFramework.Tests/Mocks/TestSoftDeleteEntityRepository.cs b/NetCoreEntityFramework.Tests/Mocks/TestSoftDeleteEntityRepository.cs index 32afaff..5239a0e 100644 --- a/NetCoreEntityFramework.Tests/Mocks/TestSoftDeleteEntityRepository.cs +++ b/NetCoreEntityFramework.Tests/Mocks/TestSoftDeleteEntityRepository.cs @@ -4,7 +4,7 @@ namespace Nodes.NetCore.EntityFramework.Tests.Mocks { public class TestSoftDeleteEntityRepository : EntitySoftDeleteRepository { - public TestSoftDeleteEntityRepository(TestContext context) : base(context, context.SoftDeleteTable) + public TestSoftDeleteEntityRepository(TestContext context) : base(context) { } } diff --git a/NetCoreEntityFramework/NetCoreEntityFramework.csproj b/NetCoreEntityFramework/NetCoreEntityFramework.csproj index e60e5e6..1b5f466 100644 --- a/NetCoreEntityFramework/NetCoreEntityFramework.csproj +++ b/NetCoreEntityFramework/NetCoreEntityFramework.csproj @@ -3,7 +3,7 @@ netstandard2.1 Nodes.NetCore.EntityFramework.Helpers - 2.0.0 + 2.1.0 Nodes Nodes .NET Core Entity Framework Helpers diff --git a/NetCoreEntityFramework/Repositories/EntityRepository.cs b/NetCoreEntityFramework/Repositories/EntityRepository.cs index 21c18ae..5cc1634 100644 --- a/NetCoreEntityFramework/Repositories/EntityRepository.cs +++ b/NetCoreEntityFramework/Repositories/EntityRepository.cs @@ -12,16 +12,14 @@ namespace Nodes.NetCore.EntityFramework.Repositories { public abstract class EntityRepository : IEntityRepository where TEntity : EntityBase { - protected DbSet Table { get; private set; } - private DbContext Context { get; set; } + protected DbContext Context { get; } - protected EntityRepository(DbContext context, DbSet table) + protected EntityRepository(DbContext context) { Context = context; - Table = table; } - public virtual Task Get(Guid id) => Table.FirstOrDefaultAsync(entity => entity.Id == id); + public virtual Task Get(Guid id) => BaseIncludes().FirstOrDefaultAsync(entity => entity.Id == id); public async virtual Task> GetList( [Range(1, int.MaxValue)] int page, @@ -86,7 +84,7 @@ public virtual Task Add(TEntity entity) entity.Created = now; entity.Updated = now; - Table.Add(entity); + Context.Set().Add(entity); return Task.CompletedTask; } @@ -98,7 +96,7 @@ public virtual Task Update(TEntity entity) entity.Updated = DateTime.UtcNow; - Table.Update(entity); + Context.Set().Update(entity); return Task.CompletedTask; } @@ -108,7 +106,7 @@ public virtual Task Delete(TEntity entity) if (entity == null) throw new ArgumentNullException(nameof(entity)); - Table.Remove(entity); + Context.Set().Remove(entity); return Task.FromResult(true); } @@ -126,6 +124,11 @@ public virtual async Task Delete(Guid id) return await Delete(entity); } + /// + /// Override this function to automatically include references in the result + /// + protected virtual IQueryable BaseIncludes() => Context.Set(); + protected IQueryable Paginate(IQueryable query, [Range(1, int.MaxValue)] int page, [Range(1, int.MaxValue)] int pageSize) { if (page < 1) @@ -147,7 +150,7 @@ protected IQueryable GetQueryable( Expression> orderByExpression = null, OrderBy orderBy = OrderBy.Ascending) { - IQueryable query = Table; + IQueryable query = BaseIncludes(); if (where != null) query = query.Where(where); diff --git a/NetCoreEntityFramework/Repositories/EntitySoftDeleteRepository.cs b/NetCoreEntityFramework/Repositories/EntitySoftDeleteRepository.cs index 2040020..c97981f 100644 --- a/NetCoreEntityFramework/Repositories/EntitySoftDeleteRepository.cs +++ b/NetCoreEntityFramework/Repositories/EntitySoftDeleteRepository.cs @@ -12,11 +12,11 @@ namespace Nodes.NetCore.EntityFramework.Repositories { public abstract class EntitySoftDeleteRepository : EntityRepository, IEntitySoftDeleteRepository where TEntity : EntitySoftDeleteBase { - protected EntitySoftDeleteRepository(DbContext context, DbSet table) : base(context, table) + protected EntitySoftDeleteRepository(DbContext context) : base(context) { } - public virtual Task Get(Guid id, bool includeDeleted = false) => Table.FirstOrDefaultAsync(entity => (includeDeleted || !entity.Deleted) && entity.Id == id); + public virtual Task Get(Guid id, bool includeDeleted = false) => BaseIncludes().FirstOrDefaultAsync(entity => (includeDeleted || !entity.Deleted) && entity.Id == id); public async virtual Task> GetList( [Range(1, int.MaxValue)] int page, @@ -78,7 +78,7 @@ public override Task Delete(TEntity entity) entity.DeletedAt = DateTime.UtcNow; entity.Deleted = true; - Table.Update(entity); + Context.Set().Update(entity); return Task.FromResult(true); } @@ -104,7 +104,7 @@ public virtual Task Restore(TEntity entity) entity.Deleted = false; entity.DeletedAt = null; - Table.Update(entity); + Context.Set().Update(entity); return Task.FromResult(true); }