<?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: Adeleke Ayobami</title>
    <description>The latest articles on DEV Community by Adeleke Ayobami (@matbami).</description>
    <link>https://dev.to/matbami</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%2F436101%2Fe79b5a63-2fb6-41e4-8c81-7ca0643698da.jpeg</url>
      <title>DEV Community: Adeleke Ayobami</title>
      <link>https://dev.to/matbami</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/matbami"/>
    <language>en</language>
    <item>
      <title>Circular dependency in Nest JS</title>
      <dc:creator>Adeleke Ayobami</dc:creator>
      <pubDate>Thu, 02 Mar 2023 13:13:51 +0000</pubDate>
      <link>https://dev.to/matbami/circular-dependency-in-nest-js-3764</link>
      <guid>https://dev.to/matbami/circular-dependency-in-nest-js-3764</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;I recently came about this Error while running a nest js application&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmk5cbp7653u99eyrrtlc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmk5cbp7653u99eyrrtlc.png" alt="Nest Error" width="800" height="141"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Firstly, it is important to know that what brought about circular dependency is Dependency Injection&lt;br&gt;
So to have an understanding of circular dependency, one needs to understand dependency injection&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.techtarget.com/searchapparchitecture/definition/dependency-injection" rel="noopener noreferrer"&gt;&lt;strong&gt;Dependency injection&lt;/strong&gt;&lt;/a&gt; is the process of supplying a resource that a given piece of code requires. The required resource, which is often a component of the application itself is called a &lt;strong&gt;dependency&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Basically, instead of having a class declare objects that it depends on, these objects should be fed to your class.&lt;/p&gt;

&lt;p&gt;This essentially means our code is more decoupled and testable.&lt;/p&gt;

&lt;p&gt;Even though dependency injection makes code easier to write and test, we occasionally encounter some issues one of which is &lt;strong&gt;circular dependency&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;let's picture this; &lt;br&gt;
Say we have two services; &lt;strong&gt;A&lt;/strong&gt; and &lt;strong&gt;B&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If service &lt;strong&gt;A&lt;/strong&gt;, depends on a resource in service &lt;strong&gt;B&lt;/strong&gt; and service &lt;strong&gt;B&lt;/strong&gt; depends on a resource in &lt;strong&gt;A&lt;/strong&gt; to run, then we have a circular dependency.&lt;/p&gt;

&lt;p&gt;To compile &lt;strong&gt;A&lt;/strong&gt;, you must first Compile &lt;strong&gt;B&lt;/strong&gt;, but you can't do that because &lt;strong&gt;A&lt;/strong&gt; requires &lt;strong&gt;B&lt;/strong&gt; to be compiled.&lt;/p&gt;

&lt;p&gt;circular dependencies can arise in Nest between modules and between providers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As much as possible, circular dependencies should be avoided, but in cases where you can't, nest provides a workaround&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A forward reference allows Nest to reference classes that aren't yet defined using the &lt;code&gt;forwardRef()&lt;/code&gt; utility function. This can be imported from the &lt;code&gt;@nestjs/common package&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;let's look at some code, shall we?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;user.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@injectable()
export class UserService {
constructor(
@inject(forwardRef(()=&amp;gt; BlogService))
private blogService: BlogService

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;blog.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@injectable()
export class BlogService {
constructor(
@inject(forwardRef(()=&amp;gt; UserService))
private userService: UserService

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;from the above, we can see that both User service and Blog service need each other hence a &lt;code&gt;forwardRef()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This can also be done on the module level on both sides of the association as well&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;user.module.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
@Module({
imports: [forwardRef(() =&amp;gt; BlogModule)]})
export class UserModule {}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing the dependency
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
One major problem with circular dependency is that it creates &lt;a href="https://nordicapis.com/the-difference-between-tight-coupling-and-loose-coupling/" rel="noopener noreferrer"&gt;tight couplings&lt;/a&gt; which the famous &lt;a href="https://www.freecodecamp.org/news/solid-principles-explained-in-plain-english/" rel="noopener noreferrer"&gt;SOLID principles&lt;/a&gt; preach against.&lt;/p&gt;

&lt;p&gt;In the above example, Circular Dependency can be removed by extracting the common features of both services into a new service that would simply depend on the services.&lt;/p&gt;

&lt;p&gt;In our case, a third service called &lt;strong&gt;profile service&lt;/strong&gt; is introduced to hold the necessary information relevant to both the user and blog service. This eventually breaks the cycle&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;With circular dependency&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;code&gt;UserService -&amp;gt; BlogService&lt;br&gt;
BlogService -&amp;gt; UserService&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Without circular dependency&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;UserService -&amp;gt; ProfileService&lt;br&gt;
BlogService -&amp;gt; ProfileService&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I would love to know if you've encountered circular dependency in nest js and how you resolved it 😄.&lt;br&gt;
Kindly share your thoughts in the comments below!&lt;/p&gt;

&lt;p&gt;Thanks&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>ethereum</category>
      <category>web3</category>
      <category>cryptocurrency</category>
    </item>
  </channel>
</rss>
