<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ELBADAOUY_BILAL</title>
    <description>The latest articles on DEV Community by ELBADAOUY_BILAL (@bilalengdv).</description>
    <link>https://dev.to/bilalengdv</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1077362%2F5ee17380-0873-485f-adaa-340267ed141e.jpeg</url>
      <title>DEV Community: ELBADAOUY_BILAL</title>
      <link>https://dev.to/bilalengdv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bilalengdv"/>
    <language>en</language>
    <item>
      <title>Dependency and dependency injection</title>
      <dc:creator>ELBADAOUY_BILAL</dc:creator>
      <pubDate>Mon, 26 Jun 2023 10:25:08 +0000</pubDate>
      <link>https://dev.to/bilalengdv/dependency-and-dependency-injection-2jd</link>
      <guid>https://dev.to/bilalengdv/dependency-and-dependency-injection-2jd</guid>
      <description>&lt;p&gt;Ever wondered how to effectively manage relationships between objects in object-oriented programming? Curious about a technique that can structure, describe, and optimize these connections? Well, have you heard of dependency injection? In this introduction, we'll delve into the world of dependency injection and explore its role in managing object dependencies. Ready to uncover the secrets behind this powerful concept? Let's dive in!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dependency and dependency injection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before we proceed, it's important to understand the term &lt;strong&gt;"dependency."&lt;/strong&gt; In object-oriented programming, a dependency is a relationship between two classes where one class relies on the functionality of the other. Simply put, it means that one class is dependent on the other.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rTq96ypY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4otqrqsvksibo35d7qvd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rTq96ypY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4otqrqsvksibo35d7qvd.png" alt="Image description" width="800" height="542"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In OOP, dependencies appear in the form of instance creation inside a class. Through this instance, one class can access the methods of another class. This way classes become dependent.&lt;/p&gt;

&lt;p&gt;Now, we will depict an example of a dependency in form of pseudocode. Here we create an instance of the &lt;code&gt;Email()&lt;/code&gt; class inside of &lt;code&gt;EmailService()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Email is
  method sendMessage() is
    ...

class EmailService is
  Email gmail = new Email()

  method sendMessage() is
    gmail.sendMessage()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Dependency injection&lt;/strong&gt; or &lt;strong&gt;DI&lt;/strong&gt; is the process of transferring the task of object creation to other parts of code. This means making a dependency that you can use. This allows the creation of loose coupling in our code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DI example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the first snippet, the &lt;code&gt;EmailService&lt;/code&gt; class relies on the &lt;code&gt;Email&lt;/code&gt; class. With this dependency, you cannot use different types of &lt;code&gt;Email&lt;/code&gt;. Its instance is constructed inside &lt;code&gt;EmailService&lt;/code&gt;, so you can't use different variations of it. Also, you can't test different &lt;code&gt;Email&lt;/code&gt; instances.&lt;/p&gt;

&lt;p&gt;To overcome this, let's try to use one type of DI on this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Email is
  method sendMessage()

class GmailService implements Email is
  method sendMessage() is ...
  ...

class EmailService is
  method sendMessage(Email service) is
    service.sendMessage()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, &lt;code&gt;EmailService&lt;/code&gt; relies on the interface instead of a class. There are two classes that use our &lt;code&gt;Email&lt;/code&gt; interface as a pattern and build different implementations. Object creation is now moved to other classes and the interface provides EmailService with different implementations. You can also set the implementation you want to access through the &lt;code&gt;sendMessage()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EmailService gmail = new EmailService()
gmail.sendMessage(new GmailService())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this is just the tip of the iceberg. There are different types of dependency injection that you need to take into consideration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of injection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are three main types of DI. Let's look at them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Method(Interface) Injection&lt;/strong&gt; — dependencies are passed through methods that the class can access via interface or another class. The previous snippet of pseudocode was an example of method injection.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Property(Setter) Injection&lt;/strong&gt; — dependencies are passed through a separate setter method. Implementation of this injector varies depending on the language. Here is a bare-bones example of this injection in pseudocode:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class EmailService is
  Email service

  method getService() is
    return service

  method setService(Email service) is
    this.service = service

class Main is
  EmailService email = new EmailService()
  email.setService(new GmailService())

  Email gmail = email.getService()
  gmail.sendMessage()
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Depending on the language, you can work with the &lt;code&gt;set&lt;/code&gt; method in different ways. For example, in Java, using Spring annotations, you can include parameters from config files into the &lt;code&gt;set&lt;/code&gt; method. However, this pseudocode is meant to be as simple as possible.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Constructor Injection&lt;/strong&gt; — dependencies are provided through the class constructor. Here we have our email example, where we can construct our &lt;code&gt;EmailService()&lt;/code&gt; with different implementations of &lt;code&gt;Email()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class EmailService is
  Email email

  constructor of EmailService(Email email) is
    this.email = email

  method sendMessage() is
    email.sendMessage()

class Main is
   EmailService gmail = new EmailService(new GmailService())
   gmail.sendMessage()
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why do we need DI?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;DI allows us to make code, parts of which are less interdependent on each other. Through DI, we can release some classes from the object creation process and pass them to injectors that can provide said objects. This way, one class will know nothing about the object creation process in the other. It allows you to reuse dependent classes without adjustment for each new instance of an object.&lt;/p&gt;

&lt;p&gt;This comes in handy when you need to extend and modify your code. But, it can also improve your testing process. With DI, you can provide different instances with different tweaks of the same object, you can test how your code will react to it.&lt;/p&gt;

&lt;p&gt;But, there are some disadvantages to DI. This can be addressed to the complexity of its implementation. It includes the steep learning curve for DI, problems caused by the overuse of DI, and troubles implementing DI with different frameworks and libraries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's summarize this topic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The main point of DI is to loosen coupling. It helps you work with dependencies.&lt;/li&gt;
&lt;li&gt;By using DI you can increase code flexibility and simplify the testing process.&lt;/li&gt;
&lt;li&gt;It's a complex topic with different implementations based on the scenario.&lt;/li&gt;
&lt;li&gt;DI in different languages has peculiarities that can affect how you work with it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While dependency injection can be a complex process, you now have a basic understanding of what it is and how it functions. If you're interested, there is plenty more to explore about dependency injection and its implementation. Feel free to delve deeper into the subject and expand your knowledge independently.&lt;/p&gt;

</description>
      <category>oop</category>
      <category>java</category>
      <category>springboot</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
