nest--Module可以如何设计你都知道吗?

526 阅读2分钟

 Module各司其职

  • 局部模组:就像中国美食、日式料理与美式风味三个区块,每个区块都有他们负责的范围。
  • 共享模组:在中国美食区可以使用「筷子」这个餐具,而日式料理区同样也会使用「筷子」这个餐具,然而在美式风味区就不太适合了,所以把「筷子」视为一个共享的模组,在中国美食区与日式料理区共用
nest generate module features/todo
nest generate controller features/todo
nest generate service features/todo

nest-mvc1.gif

import { Module } from '@nestjs/common';
import { TodoController } from './todo.controller';
import { TodoService } from './todo.service';
// 自动注入service和controller到module
@Module({
  controllers: [TodoController],
  providers: [TodoService]
})
export class TodoModule {}


//-----------------------
import { Injectable } from '@nestjs/common';

@Injectable()
export class TodoService {

    private todos: { id: number, title: string, description: string }[] = [
        {
            id: 1,
            title: 'Title 1',
            description: ''
        }
    ];
// 建一个gettodos方法回传todos内容
    getTodos(): { id: number, title: string, description: string }[] {
        return this.todos;
    }

}

//-------------------
import { Controller, Get } from '@nestjs/common';
import { TodoService } from './todo.service';

@Controller('todos')
export class TodoController {
// 在TodoController的constructor注入TodoService
    constructor(
        private readonly todoService: TodoService
    ) { }

    @Get()
    getAll() {
        return this.todoService.getTodos();
    }

}

共享模组 (Shared Module)

在 Nest 的世界里,预设情况下 Module 都是单例的,也就是说可以在各模组间共享同一个实例。事实上,每一个 Module 都算是共享模组,每个 Module 都具有高度的重用性

nest-mvc-common.gif


import { Injectable } from '@nestjs/common';

@Injectable()
export class TodoService {

    private todos: { id: number, title: string, description: string }[] = [
        {
            id: 1,
            title: 'Title 1',
            description: ''
        }
    ];

    getTodos(): { id: number, title: string, description: string }[] {
        return this.todos;
    }
    // 新增一个createTodo方法
    createTodo(item: { id: number, title: string, description: string }) {
        this.todos.push(item);
    }

}

//---------------------
import { Module } from '@nestjs/common';
import { TodoModule } from '../todo/todo.module';
import { CopyTodoController } from './copy-todo.controller';

// 在CopyTodoModule导入TodoModule实现共享
@Module({
  controllers: [CopyTodoController],
  imports: [TodoModule]
})
export class CopyTodoModule {}

//-------------------------
import { Body, Controller, Post } from '@nestjs/common';
import { TodoService } from '../todo/todo.service';

@Controller('copy-todo')
export class CopyTodoController {
    // 在CopyTodoController注入TodoService
    constructor(
        private readonly todoService: TodoService
    ) { }
    // 建一个方法来调用createTodo
    @Post()
    create(@Body() body: { id: number, title: string, description: string }) {
        this.todoService.createTodo(body);
        return body;
    }
}

常用模组 (Common Module)

Module 可以不含任何 Controller 与 Provider,只单纯把汇入的 Module 再汇出,这样的好处是可以把多个常用的 Module 集中在一起,其他 Module 要使用的话只需要汇入此 Module 就可以了

@Module({
  imports: [
    AModule,
    BModule
  ],
  exports: [
    AModule,
    BModule
  ],
})
export class CommonModule { }

全域模组 (Global Module)

当有 Module 要与多数 Module 共用时,会一直在各 Module 进行汇入的动作,这时候可以透过@Global提升 Module 为 全域模组

import { Module, Global } from '@nestjs/common';
import { TodoController } from './todo.controller';
import { TodoService } from './todo.service';

@Global()
@Module({
  controllers: [TodoController],
  providers: [TodoService],
  exports: [TodoService]
})
export class TodoModule { }

小结

  1. 每个 Module 都是共享模组,其遵循着「依照各模组的需求来串接」的概念来设计。 透过共享模组的方式来与其他模组共用同一个实例。
  2. 可以透过全域模组来减少汇入次数,但不该把多数模组做提升,在设计上不是很理想。
  3. 善用常用模组的方式来统一管理多个常用模组。