Aurelia DI带有打字稿接口 [英] Aurelia DI with typescript interfaces

查看:78
本文介绍了Aurelia DI带有打字稿接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了Aurelia DI的文档并查看了源代码并希望分享我想要实现的内容,这样如果我遗漏了一些明显的东西,我就会被击落。我已经查看了样本这里与Aurelia的TS,但我看不出它是如何工作的,而且缺少文档。

I've gone through the documentation of Aurelia DI and looked at the source code and wanted to share what I'm trying to achieve so that I can be shot down if I'm missing something obvious. I've looked at the samples here for TS with Aurelia but I can't see how it will work, and the docs are lacking.

我想要的是:

dataProvider.js (数据提供者界面)

dataProvider.js (the data provider interface)

export interface DataProvider {
  getData(): number;
}

itemDisplayer1.js (将消耗的一个类注入类实现接口

import {inject} from 'aurelia-framework';
import {DataProvider} from './dataProvider';

@inject(DataProvider)
export class itemDisplayer1 {
  constructor(public dataProvider: DataProvider) {
    this.dataProvider = dataProvider;
    this.data = dataProvider.getData();
  }
}

itemDisplayer2.js (另一个将使用注入类来实现接口的类

import {inject} from 'aurelia-framework';
import {DataProvider} from './dataProvider';

@inject(DataProvider)
export class itemDisplayer2 {
  constructor(public dataProvider: DataProvider) {
    this.dataProvider = dataProvider;
    this.data = dataProvider.getData();
  }
}

GoodDataProvider.js

import {DataProvider} from "./dataProvider";

export class GoodDataProvider implements DataProvider {
  data = 1;
  getData() {
    return this.data;
  }
}

BetterDataProvider.js

import {DataProvider} from "./dataProvider";

export class BetterDataProvider implements DataProvider {
  data = 2;
  getData() {
    return this.data;
  }
}

然后某处(?)我想配置应该为itemDisplayer1提供GoodDataProvider的实例,并且应该为itemDisplayer2提供BetterDataProvider(1)的实例。

And then somewhere(?) I would like to configure that itemDisplayer1 should be provided with an instance of GoodDataProvider and itemDisplayer2 should be provided with an instance of BetterDataProvider (1).

然后出现DI上下文的问题。我不知道如何使用container.createChild()。我找不到太多关于它的信息。它创建一个子容器,它将在需要时委托给父容器,但如果我创建了2个子容器并为每个子容器注册了2个提供者之一,那么itemDisplayer类将如何知道要使用哪个(不更改其定义)并注入父容器等)?

Then comes the problem of DI context. I'm not sure how to use container.createChild(). There's not much info on it that I can find. It creates a child container and it will delegate back to the parent when needed, but if I create 2 child containers and register one of the 2 providers with each child container, how would the itemDisplayer classes know which one to use (without changing their definitions and injecting in the parent container etc)?

注意:生命周期管理信息不存在于使用者或依赖者的提供者中(这通常在Aurelia DI示例中完成,看起来有点制造)。我希望能够在消费者和提供者关联时定义这一点 - 上面的点'(1)'。

Note: The lifetime management information doesn't live in the consumers or the providers of the dependencies (this is often done in the Aurelia DI examples and seems a little manufactured). I would expect this to be able to be defined when the consumers and providers are associated - point '(1)' above.

总之,这可能吗?这是近期未来的事情吗?我是否应该尝试用满足我需求的自定义容器替换Aurelia DI?

In summary, is this possible? Is this something that is on the cards for the near-ish future? Should I be trying to replace Aurelia DI with a custom container that meets my needs?

(我试图这样做的原因是为了评估js框架,框架需要演示一个成熟的DI系统,其中包含终身管理/ AOP等功能作为标准之一)

(The reason I'm trying to do this is that in order to evaluate js frameworks, the frameworks need to demonstrate a mature DI system with lifetime management/AOP etc capabilities as one of the criteria)

推荐答案

来自@ eisenbergeffect:一旦我们得到基准,DI就会得到一些内部改革。

from @eisenbergeffect: The DI is going to get some internal overhaul once we get the benchmarks written.

但是在相关的说明中,它不能用于接口,因为TypeScript编译那些在运行时离开。

But on a related note, it can’t work with interfaces because TypeScript compiles those away at runtime.

当您在DI容器中注册不同类型然后在@Inject中指定相应的唯一键时,您必须提供唯一键。 xxx)声明。钥匙可以是你喜欢的任何东西。通常人们会使用类型本身作为唯一键(这会导致一些混乱),但你可以使用字符串,数字或任何你喜欢的其他东西。

You would have to come up with unique keys when you register your different types in the DI container and then specify the appropriate unique key in the @Inject(xxx) statement. The keys can be anything you like. Normally folks use the type itself for the unique key (this causes some confusion), but you could use strings, numbers, or anything else you like.

单元测试是提供信息: https://github.com/aurelia/dependency-injection/blob /master/test/container.spec.js

the unit tests are informative also: https://github.com/aurelia/dependency-injection/blob/master/test/container.spec.js

这篇关于Aurelia DI带有打字稿接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆