依赖注射容器 - 工厂模式 [英] Dependency Injection Container - Factory Pattern

查看:113
本文介绍了依赖注射容器 - 工厂模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试了解依赖注入,并且一直在阅读并尝试编写一个类似于此的小型依赖注入容器: http://fabien.potencier.org/article/12/do-you-need-a-dependency-injection-container

I have been trying to learn about dependency injection and have been reading about and trying to code a small dependency injection container similar to this: http://fabien.potencier.org/article/12/do-you-need-a-dependency-injection-container

令人困惑的一件事是:

Is依赖注入容器只是一个荣耀的实现工厂模式?

Isnt a dependency injection container just a glorified implementation of the factory pattern?

如果是这样,为什么不这么说,为什么需要一个花哨的术语只能混淆事情。

If so, why not just call it that, why the need for a fancy term only to confuse matters.

如果不是,有人可以解释我在这里缺少什么吗?

If it isnt, can someone explain what i am missing here?

推荐答案

你必须分开依赖注入和反转控制。

You have to separate Dependency Injection and Inversion Of Control.

依赖注入是将依赖关系注入到类中,而不是让类本身对其依赖负责。

Dependency Injection is that you inject the dependencies into the class rather than letting the class itself be responsible of its dependencies.

控制反转是一种控制对象,它的生命周期。在这种情况下,由容器决定何时以及如何创建和处理对象。

Inversion Of Control is rather that something takes control over the object and it's lifetime. In this case it's up to the container to decide when and how an object should be created and disposed.

工厂模式都是关于创建一个每个呼叫的新对象。该模式本身并没有说明如何创建依赖关系。

Factory pattern are all about creating a new object at each call. The pattern itself doesn't say anything about how dependencies should be created.

这就是为什么你可以配置不同的生命周期,并使用子容器来控制生命有限的对象。

That's why you can configure different lifetimes and use child containers to control objects with limited lifetime.

我写了一篇关于这个的文章: http ://www.codeproject.com/Articles/386164/Get-injected-into-the-world-of-inverted-dependenci

I've written an article about that here: http://www.codeproject.com/Articles/386164/Get-injected-into-the-world-of-inverted-dependenci

或者如果你喜欢看更多的例子: http://www.codeproject。 com / Articles / 440665 / With-fun-with-Griffin-Container

Or if you like to see with more examples: http://www.codeproject.com/Articles/440665/Having-fun-with-Griffin-Container

这篇关于依赖注射容器 - 工厂模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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