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
Present all four approaches for grouping service functions (plain object,
module exports, namespace, abstract class) using tabs, letting developers
choose based on their background and preferences. Add tip callouts in other
locations pointing to this central reference.
Changes:
- Add Tab component to best-practice.md with 4 pattern options
- Add "Module Exports" pattern (import * as) for ES module-native approach
- Reorder patterns: Plain Object → Module Exports → Namespace → Abstract Class
- Add tip callout after Controller "Do" example linking to Service patterns
- Add tip callout in key-concept.md linking to Service patterns
- Update main service.ts example to use plain object (simplest approach)
Copy file name to clipboardExpand all lines: docs/essential/best-practice.md
+103-9Lines changed: 103 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,10 @@ head:
14
14
content: Elysia is a pattern agnostic framework, we leave the decision up to you and your team for coding patterns to use. However, we found that there are several who are using MVC pattern (Model-View-Controller) on Elysia, and found it's hard to decouple and handle types. This page is a guide to use Elysia with MVC pattern.
15
15
---
16
16
17
+
<scriptsetup>
18
+
importTabfrom'../components/fern/tab.vue'
19
+
</script>
20
+
17
21
# Best Practice
18
22
19
23
Elysia is a pattern-agnostic framework, leaving the decision of which coding patterns to use up to you and your team.
@@ -85,10 +89,9 @@ import { status } from 'elysia'
85
89
86
90
importtype { AuthModel } from'./model'
87
91
88
-
// If the class doesn't need to store a property,
89
-
// you may use `abstract class` to avoid class allocation
This example uses an abstract class, but you can also use plain objects, module exports, or namespaces. See [Service patterns](#1-abstract-away-non-request-dependent-service) for alternatives.
219
+
:::
220
+
214
221
Tying the controller to Elysia Context may lead to:
215
222
1. Loss of type integrity
216
223
2. Make it harder to test and reuse
@@ -280,16 +287,99 @@ There are 2 types of service in Elysia:
280
287
281
288
We recommend abstracting a service class/function away from Elysia.
282
289
283
-
If the service or function isn't tied to an HTTP request or doesn't access a `Context`, it's recommended to implement it as a static class or function.
290
+
If the service or function isn't tied to an HTTP request or doesn't access a `Context`, you can group related functions using several patterns depending on your preference:
Copy file name to clipboardExpand all lines: docs/key-concept.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -326,6 +326,10 @@ const app = new Elysia()
326
326
})
327
327
```
328
328
329
+
::: tip
330
+
This example uses an abstract class, but you can also use plain objects, module exports, or namespaces. See [Best Practice: Service patterns](/essential/best-practice.html#1-abstract-away-non-request-dependent-service) for alternatives.
331
+
:::
332
+
329
333
See [Best practice: MVC Controller](/essential/best-practice.html#controller).
0 commit comments