如何在typescript中使用依赖项注入?
我曾尝试在typescript代码中添加依赖项注入,但效果很好。但我需要在依赖项注入中加载express应用程序 依赖项。ts如何在typescript中使用依赖项注入?,typescript,typescript-typings,typescript2.0,typescript-generics,typescript1.5,Typescript,Typescript Typings,Typescript2.0,Typescript Generics,Typescript1.5,我曾尝试在typescript代码中添加依赖项注入,但效果很好。但我需要在依赖项注入中加载express应用程序 依赖项。ts import { injectable } from 'inversify'; @injectable() export class DependencyA{ public getName(){ return "dependencyA" } } @injectable() export class DependencyB{ p
import { injectable } from 'inversify';
@injectable()
export class DependencyA{
public getName(){
return "dependencyA"
}
}
@injectable()
export class DependencyB{
public getName(){
return "dependencyB"
}
}
import {injectable} from 'inversify';
import { DependencyA,DependencyB } from './dependencies';
@injectable()
export class Service{
dependencya: DependencyA;
dependencyb: DependencyB;
constructor(dependencya:DependencyA,dependencyb:DependencyB){
this.dependencya = dependencya;
this.dependencyb = dependencyb
}
public getAllName(){
return this.dependencya
}
}
import 'reflect-metadata'
import { Service } from './service';
import {container} from 'tsyringe';
const service = container.resolve(Service)
console.log(service.getAllName)
服务.ts
import { injectable } from 'inversify';
@injectable()
export class DependencyA{
public getName(){
return "dependencyA"
}
}
@injectable()
export class DependencyB{
public getName(){
return "dependencyB"
}
}
import {injectable} from 'inversify';
import { DependencyA,DependencyB } from './dependencies';
@injectable()
export class Service{
dependencya: DependencyA;
dependencyb: DependencyB;
constructor(dependencya:DependencyA,dependencyb:DependencyB){
this.dependencya = dependencya;
this.dependencyb = dependencyb
}
public getAllName(){
return this.dependencya
}
}
import 'reflect-metadata'
import { Service } from './service';
import {container} from 'tsyringe';
const service = container.resolve(Service)
console.log(service.getAllName)
main.ts
import { injectable } from 'inversify';
@injectable()
export class DependencyA{
public getName(){
return "dependencyA"
}
}
@injectable()
export class DependencyB{
public getName(){
return "dependencyB"
}
}
import {injectable} from 'inversify';
import { DependencyA,DependencyB } from './dependencies';
@injectable()
export class Service{
dependencya: DependencyA;
dependencyb: DependencyB;
constructor(dependencya:DependencyA,dependencyb:DependencyB){
this.dependencya = dependencya;
this.dependencyb = dependencyb
}
public getAllName(){
return this.dependencya
}
}
import 'reflect-metadata'
import { Service } from './service';
import {container} from 'tsyringe';
const service = container.resolve(Service)
console.log(service.getAllName)
如何在dependency injection中加载express app并在dependency constructor中传递参数我会确保您在整个应用程序中坚持使用inversify,并使用inversify的
容器
,而不是tsyringe
中的容器
我还想向您介绍这个软件包(),我正在为一个使用DI的基于服务的express应用程序成功地使用它
您可以创建一个InversifyExpressServer
将其传递给您的inversify容器和自定义express应用程序,如下所示
const container = new Container();
container.bind<MyServiceA>(TYPES.MyServiceA).to(MyServiceA)
container.bind<MyServiceB>(TYPES.MyServiceB).to(MyServiceB)
const app = express();
// apply middleware
app.use(middleware)
// any regular functional express routes
app.use('/', controllerFunc)
...etc etc
const server = new InversifyExpressServer(container, null, null, app);
它与现有的应用程序很好地集成。我目前正在使用这个inversify包将一个具有约100个端点的express应用程序从一个普通的js express应用程序迁移到一个ts应用程序。由于您可以通过ExpressServier
定制应用程序,因此在重构之前,您可以将所有旧路线保持完整功能
或者,如果您的项目是新的,我建议您签出Nestjs()。这是一个基于express构建的框架,受angular的启发,它有自己的内置依赖项反转