为Angular 2中的依赖项注入提供模型类的新实例 [英] Provide new instances of model class for dependency injection in Angular 2

查看:123
本文介绍了为Angular 2中的依赖项注入提供模型类的新实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,可以作为从服务器获取的某些数据的模型.该数据从一个笨拙的xml对象开始,其中的文本节点具有属性,因此我将其转换成的json格式没有简单的字符串值.相反,我有:

I have a class that serves as a model for some data I get from a server. This data starts as an unwieldy xml object where text nodes have attributes so the json format I convert it into does not have simple string values. Instead I have:

@Injectable()
export class FooString {
  _attr: string;
  value: string;
  isReadOnly(): boolean {
    return this._attr && this._attr === 'ReadOnly';
  }

  isHidden(): boolean {
    return this._attr && this._attr === 'Hid';
  }
}

然后我的模型是这样的:

Then my model is like:

@Injectable()
export class Payment {
  constructor(
    public FooId: FooString,
    public FooStat: FooString,
    public FooName: FooString ) { }
}

一切都以相同的FooString实例结束.如何获得每个实例的离散实例?

Everything ends up with the same instance of FooString. How do I get discrete instances for each of them?

我尝试过一个工厂,但是它仍然只创建一个实例:

I have tried a factory, but it still only creates a single instance:

export let fooStringProvider = provide(FooString, {
  useFactory: (): FooString => {
    console.log('in foostring factory');
    return new FooString();
  }
});

推荐答案

new FooString();
new Payment();

;-)

为什么在没有依赖项并且您不想为每个提供者维护单个实例的情况下使用DI.因此,只需使用new.

Why using DI when they don't have dependencies and you don't want to maintain single instances per provider. Therefore, just use new.

何时使用DI

使用DI代替new是正确的选择时,有一些条件:

There are a few criterias when using DI instead of new the right thing:

  • 如果您希望Angular维护和共享实例
  • 如果要使用接口或基类,但要从外部配置在运行时实际应使用的实现方式-例如在测试过程中HttpMockBackend.
  • 如果您的班级依赖于DI提供的实例和/或值
  • 如果您希望能够轻松地单独测试类( https://en.wikipedia. org/wiki/Inversion_of_control )
  • 可能是其他人...
  • If you want Angular to maintain and share instances
  • If you want to work with an interface or base class but then you want to configure from the outside what implementation should actually be used at runtime - like the MockBackend for Http during testing.
  • If you class has dependencies to instances and/or values provided by DI
  • If you want to be able to easily test classes in isolation (https://en.wikipedia.org/wiki/Inversion_of_control)
  • probably others ...

如果有很好的论据来使用DI,但是您还想要新的实例,那么您只需提供一个工厂即可.

If there are good arguments to use DI, but you also want new instances then you can just provide a factory.

此答案 https://stackoverflow.com/a/36046754/217408 包含具体示例.

This answer https://stackoverflow.com/a/36046754/217408 contains a concrete example how to do that.

使用DI通常是个好主意.没有恕我直言,强烈反对使用DI.仅当上述参数都不适用且提供工厂太麻烦时,才使用new Xxx().

Using DI is usually a good idea. There are IMHO no strong arguments against using DI. Only when none of the above arguments apply and providing factories is too cumbersome, use new Xxx() instead.

这篇关于为Angular 2中的依赖项注入提供模型类的新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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