{"id":110,"date":"2020-02-18T12:00:00","date_gmt":"2020-02-18T12:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=110"},"modified":"2024-02-08T08:08:33","modified_gmt":"2024-02-08T13:08:33","slug":"what-is-dependency-injection","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/what-is-dependency-injection\/","title":{"rendered":"What is Dependency Injection"},"content":{"rendered":"\n<p>Dependency Injection a big name for a very simple concept. Objects interact with other objects. A piece of data or an object that an object requires is its dependency. Objects can fetch dependencies on their own as in the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public MyClass() {\n  myObj = Factory.getObj();\n}<\/code><\/pre>\n\n\n\n<p>However, we can also provide the dependencies to the object so that it doesn&#8217;t have to fetch them as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public MyClass (YourClass yourObj) {\n  this.myObj = yourObj;\n}<\/code><\/pre>\n\n\n\n<p>What is the benefit of using dependency injection. Mostly ease of testing. You don&#8217;t need to mock myObj and intercept Factory call. Just pass the object as an argument<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dependency Injection a big name for a very simple concept. Objects interact with other objects. A piece of data or an object that an object requires is its dependency. Objects can fetch dependencies on their own as in the following example: However, we can also provide the dependencies to the object so that it doesn&#8217;t [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[185],"tags":[54,76],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript","tag-programming"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=110"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/110\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/110\/revisions\/111"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}