角度依赖注入实现,为什么? [英] Angular Dependency Injection Implementation, Why?

查看:55
本文介绍了角度依赖注入实现,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人这么说,请原谅我不理解这一点.

Forgive me for not understanding this if it's been said.

为什么其Dependency Injector的Angular实现通过constructor使用可注入对象?

Why does the Angular implementation of its Dependency Injector use the injectables through a constructor?

我习惯于以各种方式看到DI.即使是静态方法也很有意义(如果存在,抱歉,我还没有深入研究,到目前为止,我已经花了一周的时间).

I am used to seeing a DI in various ways. Even a static method would make sense (If that exists sorry I haven't dug that deep yet, I'm a week into it so far).

以这种方式使用它会更容易或更合乎逻辑吗?更类似于我们经常看到的DI,但仍将其传递给构造函数吗?:

Wouldn't it be easier or more logical to use it this way, more similar to a DI we see more often but still passing it in the constructor?:

// Non-Angular Example
@Component({})
class FooComponent {
  public appState: AppState;

  constructor(DI: DependencyInjector) {
    this.appState = DI.get('AppState'); 
  }

  ngOnInit() {}
}

Angular更像这样,我不确定它是否仅用于冗长或其他原因.

Angular is more like this, I'm not sure if it's for verbosity only, or if there are other reasons.

// Angular 2/4 Example 
@Component({})
class BarComponent {
  public appState: AppState;

  constructor(appState: AppState, 
              router: Router,
              etc: EtcSomething) {

  }
  ngOnInit() {}

我知道Google已经想到了这一点,我只是想了解原因和/或好处.也许我醒了,想着一些愚蠢的事情,这很明显,只是一头雾水,但我错过了.

I know Google had thought of this, I am just trying to understand the reasoning and/or benefit. Perhaps I woke up thinking about silly things and it's obvious and just went over my head but I missed it.

我希望我的要求是有道理的,我只是想知道为什么.

I hope what I'm asking makes sense, I just wonder why.

推荐答案

以这种方式使用它会更容易或更合乎逻辑吗?更类似于我们经常看到的DI,但仍将其传递给构造函数吗?

Wouldn't it be easier or more logical to use it this way, more similar to a DI we see more often but still passing it in the constructor?

示例中的模式说明实际上称为服务定位器模式.许多人都认为这种模式是反模式.

The illustration of the pattern in your example is actually called a Service locator pattern. This pattern is considered by many to be an anti-pattern.

优势

  • 服务定位器"可以充当简单的运行时链接程序.这样就可以在运行时添加代码而无需重新编译应用程序,在某些情况下甚至不必重新启动它即可.
  • 应用程序可以通过在服务定位器中有选择地添加和删除项目来在运行时优化自身.例如,应用程序可以检测到它具有比默认应用程序更好的库,可以读取可用的JPG图像,并相应地更改注册表.
  • 库或应用程序的大部分可以完全分开.它们之间的唯一链接就是注册表.

Advantages

  • The "service locator" can act as a simple run-time linker. This allows code to be added at run-time without re-compiling the application, and in some cases without having to even restart it.
  • Applications can optimize themselves at run-time by selectively adding and removing items from the service locator. For example, an application can detect that it has a better library for reading JPG images available than the default one, and alter the registry accordingly.
  • Large sections of a library or application can be completely separated. The only link between them becomes the registry.
  • 相对于系统的其余部分,注册表中放置的内容实际上是黑匣子.这使得更难检测到错误并从错误中恢复,并且可能使整个系统的可靠性降低.
  • 注册表必须是唯一的,这会使它成为并发应用程序的瓶颈.
  • 注册表可能是一个严重的安全漏洞,因为它允许外部人员将代码注入到应用程序中.
  • 注册表将隐藏类的依赖项,从而在缺少依赖项时导致运行时错误,而不是编译时错误.
  • 注册表使代码更难以维护(与使用依赖注入有关),因为不清楚何时引入重大更改.
  • 注册表使代码更难测试,因为所有测试都需要与同一个全局服务定位器类进行交互,以设置被测类的虚假依赖关系.但是,通过使用单个服务定位器接口注入应用程序类可以轻松解决这一问题.
  • Things placed in the registry are effectively black boxes with regards to the rest of the system. This makes it harder to detect and recover from their errors, and may make the system as a whole less reliable.
  • The registry must be unique, which can make it a bottleneck for concurrent applications.
  • The registry can be a serious security vulnerability, because it allows outsiders to inject code into an application.
  • The registry hides the class' dependencies, causing run-time errors instead of compile-time errors when dependencies are missing.
  • The registry makes the code more difficult to maintain (opposed to using Dependency injection), because it becomes unclear when you would be introducing a breaking change.
  • The registry makes code harder to test, since all tests need to interact with the same global service locator class to set the fake dependencies of a class under test. However, this is easily overcome by injecting application classes with a single service locator interface.

依赖注入

angular(以及其他框架)当前使用的(首选)模式.

Dependency Injection

The (preferred) pattern currently used by angular (as well as other frameworks).

优势

  • 依赖注入使客户端可以灵活配置.只有客户的行为是固定的.客户端可以执行任何支持客户端期望的内部接口的操作.
  • 依赖注入可用于将系统的配置详细信息外部化为配置文件,从而无需重新编译即可重新配置系统.可以针对需要组件的不同实现的不同情况编写单独的配置.这包括但不限于测试.
  • 由于依赖项注入不需要任何代码行为更改,因此可以将其作为重构应用于遗留代码.结果是客户端变得更加独立,并且可以使用存根或模拟其他未测试对象的模拟对象来进行单独的单元测试.这种测试的简便性通常是使用依赖项注入时首先注意到的好处.
  • 依赖注入使客户端可以删除其需要使用的具体实现的所有知识.这有助于使客户免受设计变更和缺陷的影响.它可以提高可重用性,可测试性和可维护性.
  • 减少应用程序对象中的样板代码,因为初始化或设置依赖项的所有工作均由提供程序组件处理.
  • 依赖注入允许并发或独立开发.两个开发人员可以独立开发可以互相使用的类,而只需要知道类将通过其进行通信的接口即可.插件通常是由第三方商店开发的,甚至从未与创建使用插件的产品的开发人员交谈.
  • 依赖注入减少了类与其依赖之间的耦合.

Advantages

  • Dependency injection allows a client the flexibility of being configurable. Only the client's behavior is fixed. The client may act on anything that supports the intrinsic interface the client expects.
  • Dependency injection can be used to externalize a system's configuration details into configuration files, allowing the system to be reconfigured without recompilation. Separate configurations can be written for different situations that require different implementations of components. This includes, but is not limited to, testing.
  • Because dependency injection doesn't require any change in code behavior it can be applied to legacy code as a refactoring. The result is clients that are more independent and that are easier to unit test in isolation using stubs or mock objects that simulate other objects not under test. This ease of testing is often the first benefit noticed when using dependency injection.
  • Dependency injection allows a client to remove all knowledge of a concrete implementation that it needs to use. This helps isolate the client from the impact of design changes and defects. It promotes reusability, testability and maintainability.
  • Reduction of boilerplate code in the application objects, since all work to initialize or set up dependencies is handled by a provider component.
  • Dependency injection allows concurrent or independent development. Two developers can independently develop classes that use each other, while only needing to know the interface the classes will communicate through. Plugins are often developed by third party shops that never even talk to the developers who created the product that uses the plugins.
  • Dependency Injection decreases coupling between a class and its dependency.
  • 依赖注入会创建需要构造代码提供需求配置详细信息的客户端.如果存在明显的默认设置,这可能会很麻烦.
  • 依赖注入可以使代码难以跟踪(读取),因为它会将行为与构造分开.这意味着开发人员必须参考更多文件来遵循系统的性能.
  • 依赖注入通常需要更多的前期开发工作,因为不能在需要的时间和地点召唤出正确的东西,而必须要求将其注入,然后确保已被注入.
  • 依赖注入迫使复杂性移出类,而移入类之间的链接中,这可能并不总是很理想或易于管理.
  • 具有讽刺意味的是,依赖注入可以鼓励对依赖注入框架的依赖.
  • Dependency injection creates clients that demand configuration details be supplied by construction code. This can be onerous when obvious defaults are available.
  • Dependency injection can make code difficult to trace (read) because it separates behavior from construction. This means developers must refer to more files to follow how a system performs.
  • Dependency injection typically requires more upfront development effort since one can not summon into being something right when and where it is needed but must ask that it be injected and then ensure that it has been injected.
  • Dependency injection forces complexity to move out of classes and into the linkages between classes which might not always be desirable or easily managed.
  • Ironically, dependency injection can encourage dependence on a dependency injection framework.


您可以找到有关这些模式的文章,书籍,教程和其他信息.尽管这是优先选择的问题,但还是达成了共识,即与关于这两个之间的差异,还有其他类似的问题:依赖注入和服务定位器模式之间有什么区别?.

There are also other similar questions about the differences between these 2 like this one: What's the difference between the Dependency Injection and Service Locator patterns?.

这篇关于角度依赖注入实现,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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