preface

Recently, the project team was preparing to build their own SDK component library, and the team leader talked about these two points during our internal review meeting. When I heard it for the first time, I was confused in my face. It was another upgrade for me, so I began to learn after the meeting. Let’s get down to business.

Introduction to the

Baidu Encyclopedia 👇

  • AOP (aspect oriented programming)

In the software industry, AOP for the abbreviation of Aspect Oriented Programming, meaning: section-oriented Programming, through pre-compilation and runtime dynamic proxy to achieve unified maintenance of program functions of a technology. AOP is a continuation of OOP, a hot topic in software development, and an important content in Spring framework. It is a derivative paradigm of functional programming. Using AOP, each part of the business logic can be isolated, thus reducing the degree of coupling between each part of the business logic, improving the reusability of the program, and improving the efficiency of development.

  • IoC (Inversion of Control)

Inversion of Control, or IoC, is a design principle used in object-oriented programming to reduce coupling between computer code. One of the most common is called Dependency Injection, or DI, and another is called Dependency Lookup. With inversion of control, when an object is created, it is passed a reference to the object on which it depends by an external entity that regulates all objects in the system. In other words, dependencies are injected into objects.

My understanding 👇 (if there are mistakes, please point out)

  • AOP (aspect oriented programming)

A section is a function of cutting an object into several different sides, each side corresponding to a different point. And then they build something together, like building blocks, like Lego. You can make adjustments to each face without affecting the others, and each face is a separate face that serves a single function.

  • IoC (Inversion of Control)

The dependency required by the module is not directly imported into the module itself, but obtained from the container after finding the dependency needed by the module itself. This prevents dependencies from being highly coupled to modules, in the form of plug-ins. For example, Vue is the dependency I need, and some libraries developed based on Vue, such as Element Ui, are plug-ins. I do not introduce my Vue dependencies into the plug-in, and the subsequent use of Vue instances (containers to my plug-in).

use

So what exactly is AOP useful for? Let’s start with a piece of code.

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    return this.appService.getHello(); }}Copy the code

As anyone who has used NestJS knows, this code is the template code that initializes nest projects.

Write routes in nestjs directly to the decorator @get (path?). or @xxx(path?) .

The decorator part just gets the route, and the part of my function below just deals with the business logic.

I want to get some parameter @query (‘name’) on query.

As you can see nestJS is very comfortable writing routes, we just deal with the business logic and leave the rest to decoration.

What about IoC?

The constructor of the AppController class receives the appService argument. The constructor of the AppController class receives the appService argument.

Here is the code for appService

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

@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello World! '; }}Copy the code

As you can see, our AppService class is Injectable. Here we inject AppService into the container.

Recall that the AppController code has appService in its constructor line argument, which tells the container that “I” depends on appService. When the controller is created, the container passes the required dependencies to it, which is called injection.

Of course, nestJS also needs a Module for dependency injection, which can be regarded as our container.

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [].controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
Copy the code

Nestjs I am not very familiar with, if there are mistakes also hope to point out

application

import { Injectable } from "injector"
import { Engine } from "engine"

@Injectable()
class SDKHelper {

	constructor(private engine: Engine){}doSomething() {
    		// xxx
            this.engine.xxx()
    	}
}
Copy the code

Here I’ve done a simple demo of what we’re going to do. We’re going to do a global instance management for the SDK that we introduced, which is what we call a singleton pattern, where we’re going to maintain an instance globally. The required states or variables in each component are then handed back to the container for management. Only a single logic is handled inside the component. This is also easy to bury, maintenance, expansion, etc.

At the end

First of all, I wish you a happy New Year and a happy Year of the Ox! The above is my simple understanding of AOP and IoC, if there is a mistake, but also hope that you give advice.

Reference: zhuanlan.zhihu.com/p/89579703