You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This section covers the usage of **Resource Hooks**, which is a feature of`ResourceDefinition<T>`. See the [ResourceDefinition usage guide](resource-definitions.md) for a general explanation on how to set up a `ResourceDefinition<T>`.
3
+
This section covers the usage of **Resource Hooks**, which is a feature of`ResourceDefinition<T>`. See the [ResourceDefinition usage guide](resource-definitions.md) for a general explanation on how to set up a `ResourceDefinition<T>`. For a quick start, jump right to the [Getting started: most minimal example](#getting-started-most-minimal-example) section.
3
4
4
5
By implementing resource hooks on a `ResourceDefintion<T>`, it is possible to intercept the execution of the **Resource Service Layer** (RSL) in various ways. This enables the developer to conveniently define business logic without having to override the RSL. It can be used to implement e.g.
5
6
* Authorization
@@ -8,20 +9,20 @@ By implementing resource hooks on a `ResourceDefintion<T>`, it is possible to in
8
9
* Transformation of the served data
9
10
10
11
This usage guide covers the following sections
11
-
1.[**Semantics: pipelines, actions and hooks**](#semantics-pipelines-actions-and-hooks).
12
+
1.[**Semantics: pipelines, actions and hooks**](#semantics-pipelines-actions-and-hooks)
12
13
Understanding the semantics will be helpful in identifying which hooks on `ResourceDefinition<T>` you need to implement for your use-case.
A table overview of all pipelines and involved hooks
25
26
26
27
# 1. Semantics: pipelines, actions and hooks
27
28
@@ -89,87 +90,28 @@ Any return content can be intercepted and transformed as desired by implementing
89
90
<br><br>
90
91
For an overview of all pipelines, hooks and actions, see the table below, and for more detailed information about the available hooks, see the [IResourceHookContainer<T>](https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/ab1f96d8255532461da47d290c5440b9e7e6a4a5/src/JsonApiDotNetCore/Hooks/IResourceHookContainer.cs) interface.
91
92
92
-
# 2. Hook execution overview
93
+
# 2. Basic usage
93
94
95
+
## Getting started: most minimal example
96
+
To use resource hooks, you are required to turn them on in your `startup.cs` configuration
94
97
95
-
This table below shows the involved hooks per pipeline.
options.EnableResourceHooks=true; // default is false
106
+
options.LoadDatabaseValues=false; // default is false
107
+
}
108
+
);
109
+
...
110
+
}
111
+
```
112
+
For this example, we may set `LoadDatabaseValues` to `false`. See the [Loading database values](#loading-database-values) example for more information about this option.
170
113
171
-
## Most minimal example
172
-
The simplest example does not require much explanation. This hook would triggered by any default JsonApiDotNetCore API route for `Article`.
114
+
The simplest case of resource hooks we can then implement should not require a lot of explanation. This hook would triggered by any default JsonApiDotNetCore API route for `Article`.
@@ -294,7 +236,95 @@ public class PersonResource : ResourceDefinition<Person>
294
236
```
295
237
Note that not only anonymous people will be excluded when directly performing a `GET /people`, but also when included through relationships, like `GET /articles?include=author,reviewers`. Simultaneously, `if` condition that checks for `ResourcePipeline.Get` in the `PersonResource` ensures we still get expected responses from the API when eg. creating a person with `WantsPrivacy` set to true.
296
238
297
-
# 3. Examples: advanced usage
239
+
## Loading database values
240
+
When a hook is executed for a particular resource, JsonApiDotNetCore can load the corresponding database values and provide them in the hooks. This can be useful for eg.
241
+
* having a diff between a previous and new state of a resource (for example when updating a resource)
242
+
* performing authorization rules based on the property of a resource.
243
+
244
+
For example, consider a scenario in with the following two requirements:
245
+
* We need to log all updates on resources revealing their old and new value.
246
+
* We need to check if the property `IsLocked` is set is `true`, and if so, cancel the operation.
247
+
248
+
Consider an `Article` with title *Hello there* and API user trying to update the the title of this article to *Bye bye*. The above requirements could be implemented as follows
0 commit comments