Skip to content

Commit 864f5cd

Browse files
committed
adjustments
1 parent 08b3335 commit 864f5cd

File tree

1 file changed

+41
-16
lines changed
  • 17/umbraco-cms/customizing/foundation/repositories

1 file changed

+41
-16
lines changed

17/umbraco-cms/customizing/foundation/repositories/README.md

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Repositories
22
Repositories provide a structured way to manage data operations in the Backoffice. They abstract the data access layer, allowing for easier reuse and scalability.
33

4-
Repositories create separation between domain logic and data access. By providing a known interface for data requests, we can reuse UI components across different domains. For example, we have a generic UX flow for deleting an entity. By supplying this flow with a repository that has a known interface for deletion, we can use the same UX flow to delete any entity.
4+
Repositories create separation between domain logic and data access. By providing a known interface for data requests, we can reuse UI components across different domains. For example, we have a generic UX flow for deleting an entity. By supplying this flow with a repository that has a known interface for deletion, we can use the same UX flow to delete any entity. The same applies to Trees, Collections, Workspaces, and more.
55

66
Additionally repositories can utilize different data sources depending on the application's state. These sources may include:
77

@@ -17,40 +17,68 @@ This abstraction ensures that consumers don’t need to worry about how to acces
1717

1818
### Data flow with a repository <a href="#data-flow-with-a-repository" id="data-flow-with-a-repository"></a>
1919

20-
A repository must be instantiated where it is used. It should take an [UmbController](../../umbraco-controller/README.md) as part of the constructor. This ensures that any contexts consumed in the repository, like notifications or modals, are rendered in the correct context.
20+
A repository must be instantiated where it is used. It should take an [UmbController](../../umbraco-controller/README.md) as part of the constructor. This ensures that any contexts consumed in the repository are scoped correctly.
2121

2222
A repository can be initialized directly from an element, but will often be instantiated in a [context](../../context-api/README.md), like the Workspace Context.
2323

24+
The data flow when using a repository can be illustrated as follows:
25+
2426
```text
25-
Element → (Context) → Repository → Data Source(s)
27+
(Data Source) -> Repository -> (Controller/Context) -> Element
2628
```
2729

2830
### Using an existing Repository <a href="#using-a-repository" id="using-a-repository"></a>
2931

3032
Often, you will find that data is already available and observable in a [context](./contexts/README.md). In that case, subscribing to the context [state](./states.md) will be the right approach to take. This way, you will receive all runtime updates that occur to the data throughout the session.
3133

32-
When a context with the appropriate data state is not available, reaching for a repository will ensure access to the needed information no matter the current application state.
34+
When a context with the appropriate data state is not available, reaching for a repository will ensure access to the needed data no matter the current application state.
35+
36+
In the example below, we instantiate the `UmbDocumentItemRepository` directly in a custom element to request Document data by its unique key.
3337

3438
```typescript
35-
import { UmbElementMixin } from '@umbraco-cms/backoffice/element-api';
3639
import { LitElement} from '@umbraco-cms/backoffice/external/lit';
37-
import {UmbDocumentDetailRepository} from '@umbraco-cms/backoffice/document';
40+
import { UmbElementMixin } from '@umbraco-cms/backoffice/element-api';
41+
import { UmbDocumentItemRepository } from '@umbraco-cms/backoffice/document';
3842

3943
class MyElement extends UmbElementMixin(LitElement) {
4044
...
41-
#documentRepository = new UmbDocumentDetailRepository(this);
45+
#documentItemRepository = new UmbDocumentItemRepository(this);
4246

4347
firstUpdated() {
44-
const { data, error } = await this.#documentRepository.requestByUnique('some-unique-key');
45-
console.log('The Document Data:', data);
48+
const { data, error } = await this.#documentItemRepository.requestItems(['some-unique-key', 'another-unique-key']);
49+
console.log('The Document Items:', data);
50+
// Render data in the element
4651
}
4752
...
4853
}
4954

5055
const documentRepository = new UmbDocumentDetailRepository(this);
56+
```
57+
58+
Alternatively, you can instantiate the repository in a [controller](..) or [context](../../context-api/README.md), store it in a [state](../states.md), and then consume that context in your element. This is often the preferred approach as it allows for better separation of concerns and reusability across different components.
59+
60+
```typescript
61+
import { UmbArrayState } from '@umbraco-cms/backoffice/observable-api';
62+
import { UmbControllerBase } from '@umbraco-cms/backoffice/context-api';
63+
import { UmbDocumentItemRepository } from '@umbraco-cms/backoffice/document';
5164

65+
export class MyController extends UmbControllerBase {
66+
67+
#items = new UmbArrayState<DocumentItemModel>([]);
68+
items = this.#items.asObservable();
69+
70+
#documentItemRepository = new UmbDocumentItemRepository(this);
71+
72+
load() {
73+
const { data, error } = await this.#documentItemRepository.requestItems(['some-unique-key', 'another-unique-key']);
74+
console.log('The Document Items:', data);
75+
this.#items.setValue(data ?? []);
76+
// The items state can now be observed by any element initializing this controller
77+
}
78+
}
5279
```
5380

81+
5482
### Register a custom Repository <a href="#register-a-custom-repository" id="register-a-custom-repository"></a>
5583

5684
By registering your repository in the [Extension Registry](../../extension-registry/README.md) you make it available to use in different extension kinds that requires a repository alias.
@@ -65,21 +93,18 @@ See the example below of how to register a custom repository:
6593

6694
```typescript
6795
import { umbExtensionsRegistry } from "@umbraco-cms/backoffice/extension-registry";
96+
import { UmbRepositoryBase } from "@umbraco-cms/backoffice/repository";
97+
import type { UmbTreeRepository } from "@umbraco-cms/backoffice/tree";
6898

69-
interface MyEntityDetailModel {
70-
unique: string;
71-
entityType: string;
72-
}
73-
74-
class MyEntityDetailRepository implements UmbDetailRepository<MyEntityDetailModel> {
99+
class MyTreeRepository extends UmbRepositoryBase implements UmbTreeRepository {
75100
// Implement repository methods here
76101
}
77102

78103
const repositoryManifest = {
79104
type: "repository",
80105
alias: "My.Repository.EntityDetail",
81106
name: "My Entity Detail Repository",
82-
api: MyRepository,
107+
api: MyTreeRepository,
83108
};
84109

85110
umbExtensionsRegistry.register(repositoryManifest);

0 commit comments

Comments
 (0)