@@ -67,13 +67,56 @@ export const MAIN_TOKENS = Object.freeze({
6767});
6868```
6969
70- ### Using a Service
70+ ### Injecting Dependencies
71+
72+ Services should declare dependencies via constructor injection:
73+
74+ ``` typescript
75+ import { inject , injectable } from " inversify" ;
76+ import { MAIN_TOKENS } from " ../di/tokens" ;
77+
78+ @injectable ()
79+ export class MyService {
80+ constructor (
81+ @inject (MAIN_TOKENS .OtherService )
82+ private readonly otherService : OtherService ,
83+ ) {}
84+
85+ doSomething() {
86+ return this .otherService .getData ();
87+ }
88+ }
89+ ```
90+
91+ ### Using Services in tRPC Routers
92+
93+ tRPC routers resolve services from the container:
7194
7295``` typescript
73- import { get } from " @main/di" ; // or @renderer/di
96+ import { container } from " ../../di/container" ;
97+ import { MAIN_TOKENS } from " ../../di/tokens" ;
98+
99+ const getService = () => container .get <MyService >(MAIN_TOKENS .MyService );
74100
75- const myService = get <MyService >(TOKENS .MyService );
76- myService .doSomething ();
101+ export const myRouter = router ({
102+ getData: publicProcedure .query (() => getService ().getData ()),
103+ });
104+ ```
105+
106+ ### Testing with Mocks
107+
108+ Constructor injection makes testing straightforward:
109+
110+ ``` typescript
111+ // Direct instantiation with mock
112+ const mockOtherService = { getData: vi .fn ().mockReturnValue (" test" ) };
113+ const service = new MyService (mockOtherService as OtherService );
114+
115+ // Or rebind in container for integration tests
116+ container .snapshot ();
117+ container .rebind (MAIN_TOKENS .OtherService ).toConstantValue (mockOtherService );
118+ // ... run tests ...
119+ container .restore ();
77120```
78121
79122## IPC via tRPC
@@ -85,26 +128,22 @@ We use [tRPC](https://trpc.io/) with [trpc-electron](https://github.com/jsonnull
85128``` typescript
86129// src/main/trpc/routers/my-router.ts
87130import { z } from " zod" ;
131+ import { container } from " ../../di/container" ;
132+ import { MAIN_TOKENS } from " ../../di/tokens" ;
88133import { router , publicProcedure } from " ../trpc" ;
89- import { get } from " @main/di " ;
90- import { MAIN_TOKENS } from " @main/di/tokens " ;
134+
135+ const getService = () => container . get < MyService >( MAIN_TOKENS . MyService ) ;
91136
92137export const myRouter = router ({
93138 // Query - for read operations
94139 getData: publicProcedure
95140 .input (z .object ({ id: z .string () }))
96- .query (async ({ input }) => {
97- const service = get <MyService >(MAIN_TOKENS .MyService );
98- return service .getData (input .id );
99- }),
141+ .query (({ input }) => getService ().getData (input .id )),
100142
101143 // Mutation - for write operations
102144 updateData: publicProcedure
103145 .input (z .object ({ id: z .string (), value: z .string () }))
104- .mutation (async ({ input }) => {
105- const service = get <MyService >(MAIN_TOKENS .MyService );
106- return service .updateData (input .id , input .value );
107- }),
146+ .mutation (({ input }) => getService ().updateData (input .id , input .value )),
108147});
109148```
110149
0 commit comments