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
Copy file name to clipboardExpand all lines: docs/usage/resources/hooks.md
+75-17Lines changed: 75 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
1
2
+
2
3
# Resource Hooks
3
4
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.
4
5
@@ -12,16 +13,18 @@ This usage guide covers the following sections
12
13
1.[**Semantics: pipelines, actions and hooks**](#semantics-pipelines-actions-and-hooks)
13
14
Understanding the semantics will be helpful in identifying which hooks on `ResourceDefinition<T>` you need to implement for your use-case.
14
15
2.[**Basic usage**](#basic-usage)
16
+
Some examples to get you started.
15
17
*[**Getting started: most minimal example**](#getting-started-most-minimal-example)
16
18
*[**Logging**](#logging)
17
19
*[**Transforming data with OnReturn**](#transforming-data-with-onreturn)
// You must return IEnumerable<Article> from this hook.
285
+
// This means that you could reduce the set of entities that is
286
+
// affected by this request, eg. by entityDiff.Entities.Where( ... );
287
+
entityDiff.Entities;
281
288
}
282
289
}
283
290
```
291
+
In this case the `EntityDiffPair<T>.DatabaseValue` is `null`. If you try to access all database values at once (`EntityDiff.DatabaseValues`) when it they are turned off, an exception will be thrown.
284
292
285
293
Note that database values are turned on by default. They can be turned of globally by configuring the startup as follows:
286
294
```c#
@@ -297,15 +305,15 @@ public void ConfigureServices(IServiceCollection services)
@@ -444,6 +454,54 @@ public override void BeforeImplicitUpdateRelationship(IAffectedRelationships<Per
444
454
}
445
455
}
446
456
```
457
+
458
+
## Using Resource Hooks without EF Core
459
+
460
+
If you want to use Resource Hooks without EF Core, there are several things that you need to consider that need to be met. For any resource that you want to use hooks for:
461
+
1. The corresponding resource repository must fully implement `IEntityReadRepository<TEntity, TId>`
462
+
2. If you are using custom services, you will be responsible for injecting the `IResourceHookExecutor` service into your services and call the appropriate methods. See the [hook execution overview](#hook-execution-overview) to determine which hook should be fired in which scenario.
463
+
464
+
If you are required to use the `BeforeImplicitUpdateRelationship` hook (see previous example), there is an additional requirement. For this hook, given a particular relationship, JsonApiDotNetCore needs to be able to resolve the inverse relationship. For example: if `Article` has one author (a `Person`), then it needs to be able to resolve the `RelationshipAttribute` that corresponds to the inverse relationship for the `author` property. There are two approaches :
465
+
466
+
1.**Tell JsonApiDotNetCore how to do this only for the relevant models**. If you're using the `BeforeImplicitUpdateRelationship` hook only for a small set of models, eg only for the relationship of the example, then it is easiest to provide the `inverseNavigationProperty` as follows:
2.**Tell JsonApiDotNetCore how to do this in general**. For full support, you can provide JsonApiDotNetCore with a custom service implementation of the `IInverseRelationships` interface. relationship of the example, then it is easiest to provide the `inverseNavigationProperty` as follows:
This service will then be called run once at startup and take care of the metadata that is required for `BeforeImplicitUpdateRelationship` to be supported.
500
+
501
+
*Note: don't forget to register this singleton service with the service provider.*
502
+
503
+
504
+
447
505
## Synchronizing data across microservices
448
506
If your application is built using a microservices infrastructure, it may be relevant to propagate data changes between microservices, [see this article for more information](https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/multi-container-microservice-net-applications/integration-event-based-microservice-communications). In this example, we will assume the implementation of an event bus and we will publish data consistency integration events using resource hooks.
0 commit comments