Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.
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
2 changes: 1 addition & 1 deletion NetCoreEntityFramework.Tests/Mocks/TestEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Nodes.NetCore.EntityFramework.Tests.Mocks
{
public class TestEntityRepository : EntityRepository<TestEntity>
{
public TestEntityRepository(TestContext context) : base(context, context.Table)
public TestEntityRepository(TestContext context) : base(context)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Nodes.NetCore.EntityFramework.Tests.Mocks
{
public class TestSoftDeleteEntityRepository : EntitySoftDeleteRepository<TestSoftDeleteEntity>
{
public TestSoftDeleteEntityRepository(TestContext context) : base(context, context.SoftDeleteTable)
public TestSoftDeleteEntityRepository(TestContext context) : base(context)
{
}
}
Expand Down
2 changes: 1 addition & 1 deletion NetCoreEntityFramework/NetCoreEntityFramework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<PackageId>Nodes.NetCore.EntityFramework.Helpers</PackageId>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Authors>Nodes</Authors>
<Company>Nodes</Company>
<Product>.NET Core Entity Framework Helpers</Product>
Expand Down
21 changes: 12 additions & 9 deletions NetCoreEntityFramework/Repositories/EntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ namespace Nodes.NetCore.EntityFramework.Repositories
{
public abstract class EntityRepository<TEntity> : IEntityRepository<TEntity> where TEntity : EntityBase
{
protected DbSet<TEntity> Table { get; private set; }
private DbContext Context { get; set; }
protected DbContext Context { get; }

protected EntityRepository(DbContext context, DbSet<TEntity> table)
protected EntityRepository(DbContext context)
{
Context = context;
Table = table;
}

public virtual Task<TEntity> Get(Guid id) => Table.FirstOrDefaultAsync(entity => entity.Id == id);
public virtual Task<TEntity> Get(Guid id) => BaseIncludes().FirstOrDefaultAsync(entity => entity.Id == id);

public async virtual Task<IEnumerable<TEntity>> GetList(
[Range(1, int.MaxValue)] int page,
Expand Down Expand Up @@ -86,7 +84,7 @@ public virtual Task Add(TEntity entity)
entity.Created = now;
entity.Updated = now;

Table.Add(entity);
Context.Set<TEntity>().Add(entity);

return Task.CompletedTask;
}
Expand All @@ -98,7 +96,7 @@ public virtual Task Update(TEntity entity)

entity.Updated = DateTime.UtcNow;

Table.Update(entity);
Context.Set<TEntity>().Update(entity);

return Task.CompletedTask;
}
Expand All @@ -108,7 +106,7 @@ public virtual Task<bool> Delete(TEntity entity)
if (entity == null)
throw new ArgumentNullException(nameof(entity));

Table.Remove(entity);
Context.Set<TEntity>().Remove(entity);

return Task.FromResult(true);
}
Expand All @@ -126,6 +124,11 @@ public virtual async Task<bool> Delete(Guid id)
return await Delete(entity);
}

/// <summary>
/// Override this function to automatically include references in the result
/// </summary>
protected virtual IQueryable<TEntity> BaseIncludes() => Context.Set<TEntity>();

protected IQueryable<TEntity> Paginate(IQueryable<TEntity> query, [Range(1, int.MaxValue)] int page, [Range(1, int.MaxValue)] int pageSize)
{
if (page < 1)
Expand All @@ -147,7 +150,7 @@ protected IQueryable<TEntity> GetQueryable(
Expression<Func<TEntity, object>> orderByExpression = null,
OrderBy orderBy = OrderBy.Ascending)
{
IQueryable<TEntity> query = Table;
IQueryable<TEntity> query = BaseIncludes();

if (where != null)
query = query.Where(where);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace Nodes.NetCore.EntityFramework.Repositories
{
public abstract class EntitySoftDeleteRepository<TEntity> : EntityRepository<TEntity>, IEntitySoftDeleteRepository<TEntity> where TEntity : EntitySoftDeleteBase
{
protected EntitySoftDeleteRepository(DbContext context, DbSet<TEntity> table) : base(context, table)
protected EntitySoftDeleteRepository(DbContext context) : base(context)
{
}

public virtual Task<TEntity> Get(Guid id, bool includeDeleted = false) => Table.FirstOrDefaultAsync(entity => (includeDeleted || !entity.Deleted) && entity.Id == id);
public virtual Task<TEntity> Get(Guid id, bool includeDeleted = false) => BaseIncludes().FirstOrDefaultAsync(entity => (includeDeleted || !entity.Deleted) && entity.Id == id);

public async virtual Task<IEnumerable<TEntity>> GetList(
[Range(1, int.MaxValue)] int page,
Expand Down Expand Up @@ -78,7 +78,7 @@ public override Task<bool> Delete(TEntity entity)
entity.DeletedAt = DateTime.UtcNow;
entity.Deleted = true;

Table.Update(entity);
Context.Set<TEntity>().Update(entity);

return Task.FromResult(true);
}
Expand All @@ -104,7 +104,7 @@ public virtual Task<bool> Restore(TEntity entity)
entity.Deleted = false;
entity.DeletedAt = null;

Table.Update(entity);
Context.Set<TEntity>().Update(entity);

return Task.FromResult(true);
}
Expand Down