{"id":179637,"date":"2021-02-16T16:00:53","date_gmt":"2021-02-16T13:00:53","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-is-dependency-injection-in-programming-cloudsavvy-it\/"},"modified":"2021-02-16T16:00:53","modified_gmt":"2021-02-16T13:00:53","slug":"what-is-dependency-injection-in-programming-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-is-dependency-injection-in-programming-cloudsavvy-it\/","title":{"rendered":"#What Is Dependency Injection in Programming? \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2578307977f\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2578307977f\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-dependency-injection-in-programming-cloudsavvy-it\/#A_Basic_Example\" >A Basic Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-dependency-injection-in-programming-cloudsavvy-it\/#Benefits_of_Dependency_Injection\" >Benefits of Dependency Injection<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-dependency-injection-in-programming-cloudsavvy-it\/#Impacts_on_Testing\" >Impacts on Testing<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-dependency-injection-in-programming-cloudsavvy-it\/#Loose_Coupling\" >Loose Coupling<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-dependency-injection-in-programming-cloudsavvy-it\/#Single_Reponsibility_Principle\" >Single Reponsibility Principle<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-dependency-injection-in-programming-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What Is Dependency Injection in Programming? \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9630\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/7303e83555703919cdd077d379a2bb7b\/p\/uploads\/2021\/02\/f7bb5f95.jpg\" alt=\"Photo of code on a computer monitor\" width=\"1920\" height=\"1032\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Dependency injection is a software engineering technique where objects are passed instances of the other objects they depend on. Instead of reaching to fetch outside dependencies themselves, objects should directly receive everything they need to function.<\/p>\n<h2 id=\"a-basic-example\"><span class=\"ez-toc-section\" id=\"A_Basic_Example\"><\/span>A Basic Example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The term \u201cdependency injection\u201d and its definition can seem complex. In practice, it\u2019s a simple concept. Let\u2019s look at two <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>roaches to creating a basic object. We\u2019re using PHP here but the concepts apply to all object-oriented codebases.<\/p>\n<p><strong>Without Dependency Injection<\/strong><\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> UserCreator <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">protected<\/span> Mailer <span class=\"re0\">$Mailer<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n        <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Mailer<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Mailer<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> create<span class=\"br0\">(<\/span>string username<span class=\"sy0\">,<\/span> string <span class=\"re0\">$email<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void <span class=\"br0\">{<\/span>\n        <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Mailer<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">send<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$email<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">\"Your account\"<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">\"Welcome.\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><strong>With Dependency Injection<\/strong><\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> UserCreator <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">protected<\/span> Mailer <span class=\"re0\">$Mailer<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>Mailer <span class=\"re0\">$mailer<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n        <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Mailer<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$Mailer<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> create<span class=\"br0\">(<\/span>string <span class=\"re0\">$username<\/span><span class=\"sy0\">,<\/span> string <span class=\"re0\">$email<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void <span class=\"br0\">{<\/span>\n        <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Mailer<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">send<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$email<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">\"Your account\"<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">\"Welcome.\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The difference is subtle but significant. Both classes have a <em>dependency<\/em> on a <code>Mailer<\/code> instance. The first class constructs its own <code>Mailer<\/code>, whereas the second works with a <code>Mailer<\/code> provided by the outside world. The <code>Mailer<\/code> dependency has been <em>injected<\/em> into the class.<\/p>\n<h2 id=\"benefits-of-dependency-injection\"><span class=\"ez-toc-section\" id=\"Benefits_of_Dependency_Injection\"><\/span>Benefits of Dependency Injection<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The main advantage of dependency injection is the decoupling of classes and their dependencies that it provides. Dependency injection is a form of <em>inversion of control<\/em> \u2013 instead of classes controlling their own dependencies, they work with instances provided by their outside environment.<\/p>\n<p>In more concrete terms, injecting your dependencies simplifies changing those dependencies in the future. In a real codebase, <code>Mailer<\/code> in our example above would probably be an interface with implementations such as <code>SendmailMailer<\/code>, <code>SendGridMailer<\/code> and <code>FakeMailer<\/code>.<\/p>\n<p>Taking the first example, having classes directly construct one of the <code>Mailer<\/code> instances results in extra work if you then need to replace that <code>Mailer<\/code> implementation. With dependency injection, you can type-hint the <code>Mailer<\/code> interface to accept any compatible implementation. The implementation to use is determined by the outside world.<\/p>\n<p>Most of the time, you\u2019ll be using dependency injection in conjunction with a <em>dependency injection container<\/em>. Containers usually integrate with the application framework you\u2019re using. They automatically resolve and inject class dependencies.<\/p>\n<p>Asking a container for a <code>UserCreator<\/code> would first construct a <code>Mailer<\/code> instance. This would be passed to the <code>UserCreator<\/code> via its constructor parameter. You \u201cwire\u201d the container to define the implementation to use when an interface is typehinted. Under this model, changing the active <code>Mailer<\/code> across the entire codebase only requires rewiring the container. This can be one line of code in the container\u2019s configuration.<\/p>\n<h2 id=\"impacts-on-testing\"><span class=\"ez-toc-section\" id=\"Impacts_on_Testing\"><\/span>Impacts on Testing<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Dependency injection simplifies mocking your dependencies when testing. Because dependencies are sourced externally to the class, you can provide a fake implementation in your unit tests:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> FakeMailer <span class=\"kw2\">implements<\/span> Mailer <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> send<span class=\"br0\">(<\/span>string <span class=\"re0\">$to<\/span><span class=\"sy0\">,<\/span> string <span class=\"re0\">$subject<\/span><span class=\"sy0\">,<\/span> string <span class=\"re0\">$body<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>We don\u2019t need to send the welcome email when testing our <code>UserCreator<\/code>. Nonetheless, it would be unavoidable in the first example, where <code>UserCreator<\/code> always constructs its own live <code>Mailer<\/code>. With dependency injection, we can provide a special <code>Mailer<\/code> which satisfies the interface\u2019s contract but eliminates the side effects.<\/p>\n<h2 id=\"loose-coupling\"><span class=\"ez-toc-section\" id=\"Loose_Coupling\"><\/span>Loose Coupling<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Dependency injection isn\u2019t about eliminating dependencies altogether \u2013 they\u2019ll always be there, but they should be loosely coupled. Think of a dependency as something that is attached for a period of time, not a fixture that\u2019s permanently glued on.<\/p>\n<p>Tight coupling as seen in the first example makes for inflexible code that\u2019s difficult to relocate. Dependencies become opaque to outside observers such as test runners. Typehinting interfaces passed into your class keeps your dependencies as loosely coupled as possible.<\/p>\n<h2 id=\"single-reponsibility-principle\"><span class=\"ez-toc-section\" id=\"Single_Reponsibility_Principle\"><\/span>Single Reponsibility Principle<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A final advantage of dependency injection is its ability to help you adhere to the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Single-responsibility_principle\">Single Responsibility Principle<\/a>. This states that each class should have responsibility for a single self-contained unit of functionality in your codebase.<\/p>\n<p>Without injection, classes not only provide functionality but also construct their own dependencies. This requires each class to possess detailed knowledge about the requirements of other sub-systems. With injection, the class\u2019 knowledge of the outside world is limited to the contracts provided by the interfaces it\u2019s dependent on.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Dependency injection makes it easier to maintain object-oriented codebases. Classes which construct their own dependencies are usually a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/martinfowler.com\/bliki\/CodeSmell.html\">code smell<\/a>. They end up tightly coupled to their surroundings and are tricky to test.<\/p>\n<p>Hoisting dependencies out of the classes that use them inverts control and creates a stronger separation of concerns. You can think of this as akin to the removal of hardcoded constants: you\u2019d never write a database schema name into your source, as it should reside in a configuration file.<\/p>\n<p>The implementation of <code>Mailer<\/code> is a configuration detail too. <code>UserCreator<\/code> shouldn\u2019t concern itself with setting up a mail system. The chances are you\u2019ll be wanting to reuse the same system elsewhere in your codebase. Configuration changes more frequently than code; you\u2019ll likely want to change the way mail is sent long before you revise the business requirement that an email is sent when a user signs up.<\/p>\n<p>In summary, dependency injection encourages your components to ask for the functionality they need, instead of grabbing it piecemeal on a case-by-case basis. Application-level instance wiring via a container exposes dependencies for what they are: configuration details which are liable to change. Code your classes to accept abstractions from the environment, instead of concrete implementations constructed internally.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/9620\/what-is-dependency-injection-in-programming\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What Is Dependency Injection in Programming? \u2013 CloudSavvy IT&#8221; Dependency injection is a software engineering technique where objects are passed instances of the other objects they depend on. Instead of reaching to fetch outside dependencies themselves, objects should directly receive everything they need to function. A Basic Example The term \u201cdependency injection\u201d and its definition&#8230;<\/p>\n","protected":false},"author":1,"featured_media":179638,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/7303e83555703919cdd077d379a2bb7b\/p\/uploads\/2021\/02\/f7bb5f95.jpg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-179637","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/179637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=179637"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/179637\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/179638"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=179637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=179637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=179637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}