Inject Service in Controller

Now that UserService is ready, let’s inject it into a controller.

What is Dependency Injection?

Dependency injection (DI) lets the framework create and provide dependencies for you.

class UserController {
async list() {
const userService = new UserService();
return userService.getUsers();
}
}
@Controller('/users')
export class UserController {
@Inject()
userService: UserService;
async list() {
return this.userService.getUsers();
}
}

Build UserController

import { Controller, Get, Query, Param, Inject } from '@midwayjs/core';
import { UserService } from '../service/user.service';
@Controller('/api/users')
export class UserController {
@Inject()
userService: UserService;
@Get('/')
async list() {
const users = await this.userService.getUsers();
return { success: true, data: users };
}
@Get('/:id')
async getOne(@Param('id') id: string) {
const user = await this.userService.getUserById(parseInt(id));
if (!user) {
return { success: false, message: 'User not found' };
}
return { success: true, data: user };
}
@Get('/search')
async search(@Query('keyword') keyword: string) {
const users = await this.userService.searchUsers(keyword || '');
return { success: true, data: users, count: users.length };
}
}

Routing recap

MethodPathDescription
GET/api/usersList all users
GET/api/users/:idGet one user
GET/api/users/search?keyword=...Search users

Multiple injections

A controller can inject multiple services with @Inject().

Summary

  • Use @Inject() to request dependencies
  • Let Midway manage lifecycle and reuse instances
  • Keep controller thin and delegate logic to service
Powered by WebContainers
Files
Preparing Environment
  • npm install
  • npm run dev