Skip to content

Commit 48495d3

Browse files
committed
refactor: regroupe hook container and executors
1 parent e509956 commit 48495d3

File tree

2 files changed

+144
-114
lines changed

2 files changed

+144
-114
lines changed

src/JsonApiDotNetCore/Hooks/IResourceHookContainer.cs

Lines changed: 87 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,39 @@ public interface IResourceHookContainer { }
1212
/// <summary>
1313
/// Implement this interface to implement business logic hooks on <see cref="ResourceDefinition{T}"/>.
1414
/// </summary>
15-
public interface IResourceHookContainer<TResource> : IBeforeHooks<TResource>, IAfterHooks<TResource>, IOnHooks<TResource>, IResourceHookContainer where TResource : class, IIdentifiable { }
15+
public interface IResourceHookContainer<TResource>
16+
: IReadHookContainer<TResource>, IDeleteHookContainer<TResource>, ICreateHookContainer<TResource>,
17+
IUpdateHookContainer<TResource>, IOnReturnHookContainer<TResource>, IResourceHookContainer
18+
where TResource : class, IIdentifiable { }
1619

1720
/// <summary>
18-
/// Wrapper interface for all Before hooks.
21+
/// Read hooks container
1922
/// </summary>
20-
public interface IBeforeHooks<TResource> where TResource : class, IIdentifiable
23+
public interface IReadHookContainer<TResource> where TResource : class, IIdentifiable
24+
{
25+
/// <summary>
26+
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/>
27+
/// layer just before reading entities of type <typeparamref name="TResource"/>.
28+
/// </summary>
29+
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
30+
/// <param name="isIncluded">Indicates whether the to be queried entities are the main request entities or if they were included</param>
31+
/// <param name="stringId">The string id of the requested entity, in the case of <see cref="ResourcePipeline.GetSingle"/></param>
32+
void BeforeRead(ResourcePipeline pipeline, bool isIncluded = false, string stringId = null);
33+
/// <summary>
34+
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/>
35+
/// layer just after reading entities of type <typeparamref name="TResource"/>.
36+
/// </summary>
37+
/// <param name="entities">The unique set of affected entities.</param>
38+
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
39+
/// <param name="isIncluded">A boolean to indicate whether the entities in this hook execution are the main entities of the request,
40+
/// or if they were included as a relationship</param>
41+
void AfterRead(HashSet<TResource> entities, ResourcePipeline pipeline, bool isIncluded = false);
42+
}
43+
44+
/// <summary>
45+
/// Create hooks container
46+
/// </summary>
47+
public interface ICreateHookContainer<TResource> where TResource : class, IIdentifiable
2148
{
2249
/// <summary>
2350
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/>
@@ -40,14 +67,27 @@ public interface IBeforeHooks<TResource> where TResource : class, IIdentifiable
4067
/// <param name="entities">The unique set of affected entities.</param>
4168
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
4269
IEnumerable<TResource> BeforeCreate(IEntityHashSet<TResource> entities, ResourcePipeline pipeline);
70+
4371
/// <summary>
4472
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/>
45-
/// layer just before reading entities of type <typeparamref name="TResource"/>.
73+
/// layer just after creation of entities of type <typeparamref name="TResource"/>.
74+
/// <para />
75+
/// If relationships were created with the created entities, this will
76+
/// be reflected by the corresponding NavigationProperty being set.
77+
/// For each of these relationships, the <see cref="ResourceDefinition{T}.AfterUpdateRelationship(IRelationshipsDictionary{T}, ResourcePipeline)"/>
78+
/// hook is fired after the execution of this hook.
4679
/// </summary>
80+
/// <returns>The transformed entity set</returns>
81+
/// <param name="entities">The unique set of affected entities.</param>
4782
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
48-
/// <param name="isIncluded">Indicates whether the to be queried entities are the main request entities or if they were included</param>
49-
/// <param name="stringId">The string id of the requested entity, in the case of <see cref="ResourcePipeline.GetSingle"/></param>
50-
void BeforeRead(ResourcePipeline pipeline, bool isIncluded = false, string stringId = null);
83+
void AfterCreate(HashSet<TResource> entities, ResourcePipeline pipeline);
84+
}
85+
86+
/// <summary>
87+
/// update hooks container
88+
/// </summary>
89+
public interface IUpdateHookContainer<TResource> where TResource : class, IIdentifiable
90+
{
5191
/// <summary>
5292
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/>
5393
/// layer just before updating entities of type <typeparamref name="TResource"/>.
@@ -76,26 +116,6 @@ public interface IBeforeHooks<TResource> where TResource : class, IIdentifiable
76116
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
77117
IEnumerable<TResource> BeforeUpdate(IDiffableEntityHashSet<TResource> entities, ResourcePipeline pipeline);
78118

79-
/// <summary>
80-
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/>
81-
/// layer just before deleting entities of type <typeparamref name="TResource"/>.
82-
/// <para />
83-
/// For the <see cref="ResourcePipeline.Delete"/> pipeline,
84-
/// <paramref name="entities" /> will typically contain one entity.
85-
/// <para />
86-
/// The returned <see cref="IEnumerable{TResource}"/> may be a subset
87-
/// of <paramref name="entities"/>, in which case the operation of the
88-
/// pipeline will not be executed for the omitted entities.
89-
/// <para />
90-
/// If by the deletion of these entities any other entities are affected
91-
/// implicitly by the removal of their relationships (eg
92-
/// in the case of an one-to-one relationship), the <see cref="ResourceDefinition{T}.BeforeImplicitUpdateRelationship"/>
93-
/// hook is fired for these entities.
94-
/// </summary>
95-
/// <returns>The transformed entity set</returns>
96-
/// <param name="entities">The unique set of affected entities.</param>
97-
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
98-
IEnumerable<TResource> BeforeDelete(IEntityHashSet<TResource> entities, ResourcePipeline pipeline);
99119
/// <summary>
100120
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/>
101121
/// layer just before updating relationships to entities of type <typeparamref name="TResource"/>.
@@ -116,6 +136,28 @@ public interface IBeforeHooks<TResource> where TResource : class, IIdentifiable
116136
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
117137
/// <param name="entitiesByRelationship">A helper that groups the entities by the affected relationship</param>
118138
IEnumerable<string> BeforeUpdateRelationship(HashSet<string> ids, IRelationshipsDictionary<TResource> entitiesByRelationship, ResourcePipeline pipeline);
139+
140+
/// <summary>
141+
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/>
142+
/// layer just after updating entities of type <typeparamref name="TResource"/>.
143+
/// <para />
144+
/// If relationships were updated with the updated entities, this will
145+
/// be reflected by the corresponding NavigationProperty being set.
146+
/// For each of these relationships, the <see cref="ResourceDefinition{T}.AfterUpdateRelationship(IRelationshipsDictionary{T}, ResourcePipeline"/>
147+
/// hook is fired after the execution of this hook.
148+
/// </summary>
149+
/// <param name="entities">The unique set of affected entities.</param>
150+
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
151+
void AfterUpdate(HashSet<TResource> entities, ResourcePipeline pipeline);
152+
153+
/// <summary>
154+
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/> layer
155+
/// just after a relationship was updated.
156+
/// </summary>
157+
/// <param name="entitiesByRelationship">Relationship helper.</param>
158+
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
159+
void AfterUpdateRelationship(IRelationshipsDictionary<TResource> entitiesByRelationship, ResourcePipeline pipeline);
160+
119161
/// <summary>
120162
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/>
121163
/// layer just before implicitly updating relationships to entities of type <typeparamref name="TResource"/>.
@@ -138,44 +180,31 @@ public interface IBeforeHooks<TResource> where TResource : class, IIdentifiable
138180
}
139181

140182
/// <summary>
141-
/// Wrapper interface for all After hooks.
183+
/// Delete hooks container
142184
/// </summary>
143-
public interface IAfterHooks<TResource> where TResource : class, IIdentifiable
185+
public interface IDeleteHookContainer<TResource> where TResource : class, IIdentifiable
144186
{
145187
/// <summary>
146188
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/>
147-
/// layer just after creation of entities of type <typeparamref name="TResource"/>.
189+
/// layer just before deleting entities of type <typeparamref name="TResource"/>.
148190
/// <para />
149-
/// If relationships were created with the created entities, this will
150-
/// be reflected by the corresponding NavigationProperty being set.
151-
/// For each of these relationships, the <see cref="ResourceDefinition{T}.AfterUpdateRelationship(IRelationshipsDictionary{T}, ResourcePipeline)"/>
152-
/// hook is fired after the execution of this hook.
153-
/// </summary>
154-
/// <returns>The transformed entity set</returns>
155-
/// <param name="entities">The unique set of affected entities.</param>
156-
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
157-
void AfterCreate(HashSet<TResource> entities, ResourcePipeline pipeline);
158-
/// <summary>
159-
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/>
160-
/// layer just after reading entities of type <typeparamref name="TResource"/>.
161-
/// </summary>
162-
/// <param name="entities">The unique set of affected entities.</param>
163-
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
164-
/// <param name="isIncluded">A boolean to indicate whether the entities in this hook execution are the main entities of the request,
165-
/// or if they were included as a relationship</param>
166-
void AfterRead(HashSet<TResource> entities, ResourcePipeline pipeline, bool isIncluded = false);
167-
/// <summary>
168-
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/>
169-
/// layer just after updating entities of type <typeparamref name="TResource"/>.
191+
/// For the <see cref="ResourcePipeline.Delete"/> pipeline,
192+
/// <paramref name="entities" /> will typically contain one entity.
170193
/// <para />
171-
/// If relationships were updated with the updated entities, this will
172-
/// be reflected by the corresponding NavigationProperty being set.
173-
/// For each of these relationships, the <see cref="ResourceDefinition{T}.AfterUpdateRelationship(IRelationshipsDictionary{T}, ResourcePipeline"/>
174-
/// hook is fired after the execution of this hook.
194+
/// The returned <see cref="IEnumerable{TResource}"/> may be a subset
195+
/// of <paramref name="entities"/>, in which case the operation of the
196+
/// pipeline will not be executed for the omitted entities.
197+
/// <para />
198+
/// If by the deletion of these entities any other entities are affected
199+
/// implicitly by the removal of their relationships (eg
200+
/// in the case of an one-to-one relationship), the <see cref="ResourceDefinition{T}.BeforeImplicitUpdateRelationship"/>
201+
/// hook is fired for these entities.
175202
/// </summary>
203+
/// <returns>The transformed entity set</returns>
176204
/// <param name="entities">The unique set of affected entities.</param>
177205
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
178-
void AfterUpdate(HashSet<TResource> entities, ResourcePipeline pipeline);
206+
IEnumerable<TResource> BeforeDelete(IEntityHashSet<TResource> entities, ResourcePipeline pipeline);
207+
179208
/// <summary>
180209
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/>
181210
/// layer just after deletion of entities of type <typeparamref name="TResource"/>.
@@ -184,19 +213,12 @@ public interface IAfterHooks<TResource> where TResource : class, IIdentifiable
184213
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
185214
/// <param name="succeeded">If set to <c>true</c> if the deletion was succeeded in the repository layer.</param>
186215
void AfterDelete(HashSet<TResource> entities, ResourcePipeline pipeline, bool succeeded);
187-
/// <summary>
188-
/// Implement this hook to run custom logic in the <see cref=" DefaultResourceService{T}"/> layer
189-
/// just after a relationship was updated.
190-
/// </summary>
191-
/// <param name="entitiesByRelationship">Relationship helper.</param>
192-
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
193-
void AfterUpdateRelationship(IRelationshipsDictionary<TResource> entitiesByRelationship, ResourcePipeline pipeline);
194216
}
195217

196218
/// <summary>
197-
/// Wrapper interface for all on hooks.
219+
/// On return hook container
198220
/// </summary>
199-
public interface IOnHooks<TResource> where TResource : class, IIdentifiable
221+
public interface IOnReturnHookContainer<TResource> where TResource : class, IIdentifiable
200222
{
201223
/// <summary>
202224
/// Implement this hook to transform the result data just before returning

0 commit comments

Comments
 (0)