{"id":1867,"date":"2016-10-11T08:44:59","date_gmt":"2016-10-11T08:44:59","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1867"},"modified":"2019-02-25T05:20:52","modified_gmt":"2019-02-25T05:20:52","slug":"dependency-injection","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/dependency-injection\/","title":{"rendered":"Dependency Injection"},"content":{"rendered":"<p><strong>Dependency Injection<\/strong><\/p>\n<p><strong>What is Dependency<\/strong><\/p>\n<pre>\r\nQuick Example:EMPLOYEE OBJECT WHEN CREATED,\r\n              IT WILL AUTOMATICALLY CREATE ADDRESS OBJECT\r\n   (if address is defines as dependency by Employee object)\r\n<\/pre>\n<p>Now in the above <strong>EMPLOYEE<\/strong> Class is dependent on <strong>ADDRESS<\/strong> Class.<\/p>\n<p><em>You can not create the Address Object unless you create Employee  Object<\/em><\/p>\n<p>EMPLOYEE Class is <strong>dependent <\/strong> and ADDRESS Class is <strong>dependency<\/strong><\/p>\n<p><strong>What is the purpose of DI?<\/strong><br \/>\nWith dependency injection, objects don&#8217;t define their dependencies themselves, the dependencies are injected to them as needed.The purpose of Dependency Injection is to reduce coupling in your application to make it more flexible and easier to test.<\/p>\n<p><strong>How does it benefit ?<\/strong> The objects don&#8217;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.<\/p>\n<p><strong>When to use Dependency Injection<\/strong><br \/>\nOne of the most compelling reasons for DI is to allow easier unit testing without having to hit a database and worry about setting up &#8216;test&#8217; data.Dependency Injection gives you the ability to test specific units of code in isolation. <\/p>\n<p>Dependency injection is basically <strong>providing the objects that an object needs (its dependencies) instead of having it construct them itself<\/strong>. It&#8217;s a very useful technique for testing, since it allows dependencies to be mocked or stubbed out.<\/p>\n<p>Dependencies can be injected into objects by many means (such as constructor injection or setter injection). One can even use specialized dependency injection frameworks (e.g Spring) to do that, but they certainly aren&#8217;t required.<\/p>\n<p><strong>Example<\/strong><br \/>\nA Car depends on wheels, engine, fuel, battery, etc. to run. Traditionally we define the brand of such dependent objects along with the definition of the Car object<\/p>\n<p><strong>Without Dependency Injection (DI):<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass Car\r\n{\r\n  private Wheel wh   = new ApolloWheel();\r\n  private Battery bt = new ExideBattery();\r\n\r\n  \/\/The rest\r\n}\r\n<\/pre>\n<p>Here, the Car object is responsible for creating the dependent objects.<\/p>\n<p>What if we want to change the type of its dependent object &#8211; say Wheel &#8211; after the initial <strong>ApolloWheel()<\/strong> punctures? We need to recreate the Car object with its new dependency say <strong>SpareWheel()<\/strong>, but only the Car manufacturer can do that.<\/p>\n<p>Then what does the Dependency Injection do us for&#8230;?<\/p>\n<p>When <strong>using dependency injection, objects are given their dependencies at run time rather than compile time (car manufacturing time)<\/strong>. So that we can now change the Wheel whenever we want. Here, the dependency (wheel) can be injected into Car at run time.<\/p>\n<p><strong>After using dependency injection:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass Car\r\n{\r\n  private Wheel wh   = &#x5B;Inject an Instance of Wheel at runtime];\r\n  private Battery bt = &#x5B;Inject an Instance of Battery at runtime];\r\n\r\n  Car(Wheel wh,Battery bt) \r\n  {\r\n      this.wh = wh;\r\n      this.bt = bt;\r\n  }\r\n\r\n  \/\/Or we can have setters\r\n  void setWheel(Wheel wh) \r\n  {\r\n      this.wh = wh;\r\n  }\r\n}\r\n<\/pre>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/Hm2fTv9wikI\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>Lets take the below example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Foo\r\n{\r\n    private Bar _bar;\r\n\r\n    public Foo(Bar bar)\r\n    {\r\n        _bar = bar;\r\n    }\r\n\r\n    public bool IsPropertyOfBarValid()\r\n    {\r\n        return _bar.SomeProperty == PropertyEnum.ValidProperty;\r\n    }\r\n}\r\n<\/pre>\n<p>without dependency injection the Bar object is dependent and tightly coupled with Foo class like below<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Foo\r\n{\r\n    private Bar _bar = new Bar();\r\n    .\r\n    . \r\n}\r\n<\/pre>\n<p>But by using dependency injection like before code you can mock the Bar Object at runtime and call the <strong>IsPropertyOfBarValid()<\/strong> method over it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dependency Injection What is Dependency Quick Example:EMPLOYEE OBJECT WHEN CREATED, IT WILL AUTOMATICALLY CREATE ADDRESS OBJECT (if address is defines as dependency by Employee object) Now in the above EMPLOYEE Class is dependent on ADDRESS Class. You can not create the Address Object unless you create Employee Object EMPLOYEE Class is dependent and ADDRESS Class&hellip; <a href=\"https:\/\/codethataint.com\/blog\/dependency-injection\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[211],"tags":[253],"class_list":["post-1867","post","type-post","status-publish","format-standard","hentry","category-theory","tag-interview"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1867"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1867\/revisions"}],"predecessor-version":[{"id":1875,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1867\/revisions\/1875"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}