开发者

What is Dependency Injection? [duplicate]

开发者 https://www.devze.com 2023-01-08 16:31 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: What is dependency injection?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

What is dependency injection?

开发者_如何转开发

Spring is the framework from where the concept Dependency Injection came to picture.

What is purpose of DI ? How does it benefit ? How is it implemented ?


Start here.

Also see A-beginners-guide-to-Dependency-Injection. (Obsolete)

Elsewhere on SO:

  • what-is-dependency-injection
  • dependency-injection-vs-factory-pattern
  • when-to-use-dependency-injection
  • difference-between-dependency-injection-and-inversion-of-control


What is the purpose of DI?

The purpose of Dependency Injection is to reduce coupling in your application to make it more flexible and easier to test.

How does it benefit?

Objects don't have hard coded dependencies. If you need to change the implementation of a dependency, all you have to do is Inject a different type of Object.

How does it implemented?

There are various methods of Dependency Injection. Check out the Wikipedia article to see examples of each. Once you understand those, you can start investigating the various Dependency Injection frameworks.


Very short,

What is the purpose of DI? With dependency injection, objects don't define their dependencies themselves, the dependencies are injected to them as needed.

How does it benefit ? The objects don't need to know where and how to get their dependencies, which results in loose coupling between objects, which makes them a lot easier to test.

How is it implemented ? Usually a container manages the lifecycle of objects and their dependencies based on a configuration file or annotations.


Try taking a look at: http://martinfowler.com/articles/injection.html


DI allows us to swap out components, improve testability and ensure that components are loosely coupled. DI allows to resolve dependencies at run time using DI containers such as Windsor Castle, Unity, Spring.net, MEF which allows applications to be extensible.


http://en.wikipedia.org/wiki/Dependency_injection

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号