Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 969a4a0

Browse files
author
Morten Turn Pedersen
authored
Merge pull request #11 from nodes-dotnet/feature/documentation
Feature/documentation
2 parents 25cd283 + 1a234f1 commit 969a4a0

File tree

5 files changed

+135
-65
lines changed

5 files changed

+135
-65
lines changed

NetCoreEntityFramework.Tests/EntityRepositoryTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ public async Task RestoreSetsDeletedFalse()
355355

356356
var restoredEntity = await _repository.Get((Guid)_deletedEntity.Id);
357357
Assert.IsTrue(success);
358-
Assert.IsFalse(restoredEntity?.Deleted);
358+
Assert.IsFalse(restoredEntity.Deleted);
359+
Assert.IsNull(restoredEntity.DeletedAt);
359360
}
360361

361362
[Test]
@@ -377,7 +378,8 @@ public async Task RestoreOnIdSetsDeletedFalse()
377378

378379
var restoredEntity = await _repository.Get(id);
379380
Assert.IsTrue(success);
380-
Assert.IsFalse(restoredEntity?.Deleted);
381+
Assert.IsFalse(restoredEntity.Deleted);
382+
Assert.IsNull(restoredEntity.DeletedAt);
381383
}
382384

383385
[Test]

NetCoreEntityFramework/NetCoreEntityFramework.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<PackageId>Nodes.NetCore.EntityFramework.Helpers</PackageId>
6-
<Version>1.1.0</Version>
6+
<Version>1.2.0</Version>
77
<Authors>Nodes</Authors>
88
<Company>Nodes</Company>
99
<Product>.NET Core Entity Framework Helpers</Product>

NetCoreEntityFramework/Repositories/EntityRepository.cs

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Nodes.NetCore.EntityFramework.Repositories
1212
{
13-
public abstract class EntityRepository<TEntity, TContext> : IDisposable where TEntity : EntityBase where TContext : DbContext
13+
public abstract class EntityRepository<TEntity, TContext> : IEntityRepository<TEntity, TContext> where TEntity : EntityBase where TContext : DbContext
1414
{
1515
protected DbSet<TEntity> Table { get; private set; }
1616
private TContext Context { get; set; }
@@ -21,26 +21,11 @@ protected EntityRepository(TContext context, DbSet<TEntity> table)
2121
Table = table;
2222
}
2323

24-
/// <summary>
25-
/// Get the entity with the given <paramref name="id"/>.
26-
/// </summary>
27-
/// <param name="id">The ID of the entity to fetch.</param>
28-
/// <param name="includeDeleted">If true, also search amongst the soft deleted entities.</param>
2924
public async Task<TEntity> Get(Guid id, bool includeDeleted = false)
3025
{
3126
return await Table.FirstOrDefaultAsync(entity => (includeDeleted || !entity.Deleted) && entity.Id == id);
3227
}
3328

34-
/// <summary>
35-
/// Get multiple entities paginated.
36-
/// </summary>
37-
/// <param name="page">Which page to fetch (1 and above).</param>
38-
/// <param name="pageSize">The size of each page (1 and above).</param>
39-
/// <param name="where">The filter expression.</param>
40-
/// <param name="orderByExpression">The expression to order by.</param>
41-
/// <param name="orderBy">To order by ascending or descending.</param>
42-
/// <param name="mode">Whether to include deleted or not.</param>
43-
/// <exception cref="ArgumentException"></exception>
4429
public async Task<IEnumerable<TEntity>> GetList(
4530
[Range(1, int.MaxValue)] int page,
4631
[Range(1, int.MaxValue)] int pageSize,
@@ -65,14 +50,6 @@ public async Task<IEnumerable<TEntity>> GetList(
6550
return await query.ToListAsync();
6651
}
6752

68-
/// <summary>
69-
/// Get multiple entities.
70-
/// </summary>
71-
/// <param name="where">The filter expression.</param>
72-
/// <param name="orderByExpression">The expression to order by.</param>
73-
/// <param name="orderBy">To order by ascending or descending.</param>
74-
/// <param name="mode">Whether to include deleted or not.</param>
75-
/// <exception cref="ArgumentException"></exception>
7653
public async Task<IEnumerable<TEntity>> GetList(
7754
Expression<Func<TEntity, bool>> where = null,
7855
Expression<Func<TEntity, object>> orderByExpression = null,
@@ -84,13 +61,7 @@ public async Task<IEnumerable<TEntity>> GetList(
8461
return await query.ToListAsync();
8562
}
8663

87-
/// <summary>
88-
/// Add the given <paramref name="entity"/> to the database.
89-
/// An ID will be generated if not provided.
90-
/// </summary>
91-
/// <param name="entity">The entity to add.</param>
92-
/// <exception cref="ArgumentNullException"></exception>
93-
public Task Add(TEntity entity)
64+
public virtual Task Add(TEntity entity)
9465
{
9566
if (entity == null)
9667
throw new ArgumentNullException(nameof(entity));
@@ -108,12 +79,7 @@ public Task Add(TEntity entity)
10879
return Task.CompletedTask;
10980
}
11081

111-
/// <summary>
112-
/// Update the given <paramref name="entity"/> with the information set.
113-
/// </summary>
114-
/// <param name="entity">The entity to update.</param>
115-
/// <exception cref="ArgumentNullException"></exception>
116-
public Task Update(TEntity entity)
82+
public virtual Task Update(TEntity entity)
11783
{
11884
if (entity == null)
11985
throw new ArgumentNullException(nameof(entity));
@@ -125,12 +91,7 @@ public Task Update(TEntity entity)
12591
return Task.CompletedTask;
12692
}
12793

128-
/// <summary>
129-
/// Soft delete the <paramref name="entity"/>.
130-
/// </summary>
131-
/// <param name="entity">The entity to soft delete.</param>
132-
/// <exception cref="ArgumentNullException"></exception>
133-
public Task<bool> Delete(TEntity entity)
94+
public virtual Task<bool> Delete(TEntity entity)
13495
{
13596
if (entity == null)
13697
throw new ArgumentNullException(nameof(entity));
@@ -143,12 +104,7 @@ public Task<bool> Delete(TEntity entity)
143104
return Task.FromResult(true);
144105
}
145106

146-
/// <summary>
147-
/// Soft the delete the entity with the given <paramref name="id"/>.
148-
/// </summary>
149-
/// <param name="id">The ID of the entity to soft delete.</param>
150-
/// <exception cref="ArgumentException"></exception>
151-
public async Task<bool> Delete(Guid id)
107+
public virtual async Task<bool> Delete(Guid id)
152108
{
153109
if (id == Guid.Empty)
154110
throw new ArgumentException($"{nameof(id)} was not set", nameof(id));
@@ -161,12 +117,7 @@ public async Task<bool> Delete(Guid id)
161117
return await Delete(entity);
162118
}
163119

164-
/// <summary>
165-
/// Restore/undelete the entity with the given <paramref name="id"/>.
166-
/// </summary>
167-
/// <param name="id">The ID of the entity to restore.</param>
168-
/// <exception cref="ArgumentException"></exception>
169-
public async Task<bool> Restore(Guid id)
120+
public virtual async Task<bool> Restore(Guid id)
170121
{
171122
if (id == Guid.Empty)
172123
throw new ArgumentException($"{nameof(id)} was not set", nameof(id));
@@ -179,17 +130,13 @@ public async Task<bool> Restore(Guid id)
179130
return await Restore(entity);
180131
}
181132

182-
/// <summary>
183-
/// Restore/undelete the given <paramref name="entity"/>.
184-
/// </summary>
185-
/// <param name="entity">The entity to restore.</param>
186-
/// <exception cref="ArgumentNullException"></exception>
187-
public Task<bool> Restore(TEntity entity)
133+
public virtual Task<bool> Restore(TEntity entity)
188134
{
189135
if (entity == null)
190136
throw new ArgumentNullException(nameof(entity));
191137

192138
entity.Deleted = false;
139+
entity.DeletedAt = null;
193140

194141
Context.Update(entity);
195142

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Nodes.NetCore.EntityFramework.Enums;
3+
using Nodes.NetCore.EntityFramework.Models;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.ComponentModel.DataAnnotations;
7+
using System.Linq.Expressions;
8+
using System.Threading.Tasks;
9+
10+
namespace Nodes.NetCore.EntityFramework.Repositories
11+
{
12+
public interface IEntityRepository<TEntity, TContext> : IDisposable where TEntity : EntityBase where TContext : DbContext
13+
{
14+
/// <summary>
15+
/// Get the entity with the given <paramref name="id"/>.
16+
/// </summary>
17+
/// <param name="id">The ID of the entity to fetch.</param>
18+
/// <param name="includeDeleted">If true, also search amongst the soft deleted entities.</param>
19+
Task<TEntity> Get(Guid id, bool includeDeleted = false);
20+
21+
/// <summary>
22+
/// Get multiple entities paginated.
23+
/// </summary>
24+
/// <param name="page">Which page to fetch (1 and above).</param>
25+
/// <param name="pageSize">The size of each page (1 and above).</param>
26+
/// <param name="where">The filter expression.</param>
27+
/// <param name="orderByExpression">The expression to order by.</param>
28+
/// <param name="orderBy">To order by ascending or descending.</param>
29+
/// <param name="mode">Whether to include deleted or not.</param>
30+
/// <exception cref="ArgumentException"></exception>
31+
Task<IEnumerable<TEntity>> GetList(
32+
[Range(1, int.MaxValue)] int page,
33+
[Range(1, int.MaxValue)] int pageSize,
34+
Expression<Func<TEntity, bool>> where = null,
35+
Expression<Func<TEntity, object>> orderByExpression = null,
36+
OrderBy orderBy = OrderBy.Ascending,
37+
GetListMode mode = GetListMode.ExcludeDeleted);
38+
39+
/// <summary>
40+
/// Get multiple entities.
41+
/// </summary>
42+
/// <param name="where">The filter expression.</param>
43+
/// <param name="orderByExpression">The expression to order by.</param>
44+
/// <param name="orderBy">To order by ascending or descending.</param>
45+
/// <param name="mode">Whether to include deleted or not.</param>
46+
/// <exception cref="ArgumentException"></exception>
47+
Task<IEnumerable<TEntity>> GetList(
48+
Expression<Func<TEntity, bool>> where = null,
49+
Expression<Func<TEntity, object>> orderByExpression = null,
50+
OrderBy orderBy = OrderBy.Ascending,
51+
GetListMode mode = GetListMode.ExcludeDeleted);
52+
53+
/// <summary>
54+
/// Add the given <paramref name="entity"/> to the database.
55+
/// An ID will be generated if not provided.
56+
/// </summary>
57+
/// <param name="entity">The entity to add.</param>
58+
/// <exception cref="ArgumentNullException"></exception>
59+
Task Add(TEntity entity);
60+
61+
/// <summary>
62+
/// Update the given <paramref name="entity"/> with the information set.
63+
/// </summary>
64+
/// <param name="entity">The entity to update.</param>
65+
/// <exception cref="ArgumentNullException"></exception>
66+
Task Update(TEntity entity);
67+
68+
/// <summary>
69+
/// Soft delete the <paramref name="entity"/>.
70+
/// </summary>
71+
/// <param name="entity">The entity to soft delete.</param>
72+
/// <exception cref="ArgumentNullException"></exception>
73+
Task<bool> Delete(TEntity entity);
74+
75+
/// <summary>
76+
/// Soft the delete the entity with the given <paramref name="id"/>.
77+
/// </summary>
78+
/// <param name="id">The ID of the entity to soft delete.</param>
79+
/// <exception cref="ArgumentException"></exception>
80+
Task<bool> Delete(Guid id);
81+
82+
/// <summary>
83+
/// Restore/undelete the entity with the given <paramref name="id"/>.
84+
/// </summary>
85+
/// <param name="id">The ID of the entity to restore.</param>
86+
/// <exception cref="ArgumentException"></exception>
87+
Task<bool> Restore(Guid id);
88+
89+
/// <summary>
90+
/// Restore/undelete the given <paramref name="entity"/>.
91+
/// </summary>
92+
/// <param name="entity">The entity to restore.</param>
93+
/// <exception cref="ArgumentNullException"></exception>
94+
Task<bool> Restore(TEntity entity);
95+
}
96+
}

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1-
# net-core-entity-framework
1+
# Entity Framework Base Package
2+
3+
This NuGet package contains a base entity definition along with an entity service which handles the update, soft delete, add etc. which automatically sets the properties on the base entity and does some basic validation. Every class is extendable, so you can fit it to your needs. The `Add`, `Delete`, `Update`, and `Restore` functions on the entity service are all `virtual`, so you can extend them to fit your needs if you e.g. need more base attributes set automatically.
4+
5+
## BaseEntity
6+
The base entity contains the following properties:
7+
8+
| Name | Type | Description |
9+
| ---- | ---- | ----------- |
10+
| Id | Guid? | The unique id of the entity |
11+
| Created | DateTime | The UTC time for when this entity was created |
12+
| Updated | DateTime | The UTC time for when this entity was last updated |
13+
| DeletedAt | DateTime? | The UTC time for when this entity was soft deleted |
14+
| Deleted | bool | Whether the entity is deleted or not |
15+
16+
## EntityRepository
17+
This is the entity service which handles the communication with the database. There is also an interface `IEntityRepository`, so you can use DI to inject it into your code.
18+
19+
| Function | Description |
20+
| -------- | ----------- |
21+
| Get | Fetch a single entity |
22+
| GetList | Get a filtered and possibly paginated list of entities |
23+
| Add | Add a new entity to the DB. The function automatically sets `Created`, `Updated`, and `Id` on the entity |
24+
| Update | Update the provided entity in the DB. The function automatically sets `Updated` |
25+
| Delete | Soft delete the entity. Sets `Deleted` to `true` and sets `Deleted` as well |
26+
| Restore | Undelete the entity. Sets `Deleted` to `false` and sets `DeletedAt` to `null` |

0 commit comments

Comments
 (0)