Dependency injection: Difference between revisions

Content deleted Content added
Add example image
m convert special characters found by Wikipedia:Typo Team/moss (via WP:JWB)
Line 2:
[[File:DependencyInjectionServiceProvider.png|alt=A diagram of an archetypical dependency injection container for the .NET platform.|thumb|Dependency injection is often used alongside specialized frameworks, known as 'containers', to facilitate program composition.]]
[[File:Dependency injection example app.svg|thumb|PetManager gets injected into PetController and PetRepository gets injected into PetManager]]
In [[software engineering]], '''dependency injection''' is a programming technique in which an [[Object (computer science)|object]] or [[Subroutine|function]] receives other objects or functions that it requires, as opposed to creating them internally. Dependency injection aims to [[separation of concerns|separate the concerns]] of constructing objects and using them, leading to [[Loose coupling|loosely coupled]] programs.<ref>{{Cite web |last=Seemann |first=Mark |title=Dependency Injection is Loose Coupling |url=http://blog.ploeh.dk/2010/04/07/DependencyInjectionisLooseCoupling/ |access-date=2015-07-28 |website=blog.ploeh.dk}}</ref><ref name="MarkSeeman2011P4" /><ref>Niko Schwarz, Mircea Lungu, Oscar Nierstrasz, “Seuss"Seuss: Decoupling responsibilities from static methods for fine-grained configurability”configurability", Journal of Object Technology, volume&nbsp;11, no.&nbsp;1 (April 2012), pp.&nbsp;3:1–23.</ref> The pattern ensures that an object or function that wants to use a given [[Service (systems architecture)|service]] should not have to know how to construct those services. Instead, the receiving "[[Client (computing)|client]]" (object or function) is provided with its dependencies by external code (an "injector"), which it is not aware of.<ref name="HollywoodPrinciple.c2">{{Cite web |title=HollywoodPrinciple |url=http://c2.com/cgi/wiki?HollywoodPrinciple |access-date=2015-07-19 |website=c2.com}}</ref> Dependency injection makes implicit dependencies explicit and helps solve the following problems:<ref>{{cite web |title=The Dependency Injection design pattern&nbsp;– Problem, Solution, and Applicability |url=http://w3sdesign.com/?gr=u01&ugr=proble |access-date=2017-08-12 |website=w3sDesign.com}}</ref>
* How can a [[Class (computer programming)|class]] be independent from the creation of the objects it depends on?
* How can an application, and the objects it uses support different configurations?
Dependency injection is often used to keep code in-line with the [[dependency inversion principle]].<ref>{{Cite web |last=Erez |first=Guy |date=2022-03-09 |title=Dependency Inversion vs. Dependency Injection |url=https://betterprogramming.pub/straightforward-simple-dependency-inversion-vs-dependency-injection-7d8c0d0ed28e |access-date=2022-12-06 |website=Medium |language=en}}</ref><ref>{{Cite web |last=Mathews |first=Sasha |date=2021-03-25 |title=You are Simply Injecting a Dependency, Thinking that You are Following the Dependency Inversion…Inversion... |url=https://levelup.gitconnected.com/you-are-simply-injecting-a-dependency-thinking-that-you-are-following-the-dependency-inversion-32632954c208 |access-date=2022-12-06 |website=Medium |language=en }}</ref>
 
In [[statically typed language]]s using dependency injection means that a client only needs to declare the [[Interface (computing)|interfaces]] of the services it uses, rather than their concrete implementations, making it easier to change which services are used at runtime without recompiling.
Line 40:
The role of injectors is to construct and connect complex object graphs, where objects may be both clients and services. The injector itself may be many objects working together, but must not be the client, as this would create a [[circular dependency]].
 
Because dependency injection separates how objects are constructed from how they are used, it often diminishes the importance of the '''<code>new</code>''' keyword found in most [[Object-oriented programming|object-oriented languages]]. Because the framework handles creating services, the programmer tends to only directly construct [[value object]]s which represents entities in the program's ___domain (such as an <code>Employee</code> object in a business app or an <code>Order</code> object in a shopping app).<ref>{{Cite web |title=To "new" or not to "new"... |url=http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/ |access-date=2015-07-18 |archive-date=2020-05-13 |archive-url=https://web.archive.org/web/20200513185005/http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/ |url-status=dead }}</ref><ref>{{Cite web |title=How to write testable code |url=http://www.loosecouplings.com/2011/01/how-to-write-testable-code-overview.html |access-date=2015-07-18 |website=www.loosecouplings.com}}</ref><ref>{{Cite web |title=Writing Clean, Testable Code |url=http://www.ethanresnick.com/blog/testableCode.html |access-date=2015-07-18 |website=www.ethanresnick.com}}</ref><ref>{{Cite web |last=Sironi |first=Giorgio |title=When to inject: the distinction between newables and injectables - Invisible to the eye |url=http://www.giorgiosironi.com/2009/07/when-to-inject-distinction-between.html |access-date=2015-07-18 |website=www.giorgiosironi.com}}</ref>
 
=== Analogy ===
Line 282:
 
=== C# ===
This sample provides an example of constructor injection in [[C#C# (programming language)|C#]].
<syntaxhighlight lang="csharp">
using System;
Line 468:
 
User struct {
Name string `'json:"name" db:"name,primarykey"`'
JoinedAt time.Time `'json:"joined_at" db:"joined_at"`'
Email string `'json:"email" db:"email"`'
}
)