Introduction

Couple of days ago, I came across a piece of code that was not just tightly coupled but also formed a circular dependency among the components. In the following code, I’ve tried to give you a gist of it (may not be the best though). The below code has two key components, DataProvider and DataValidator. The DataProvider “new” DataValidator and passes in its own instance, which is then used to fetch data, making them very tightly coupled to each other.

 

 

Circular Dependency Problem

In the above code sample, DataProvider needs DataValidator to perform some data validation. However, the later in turn needs the DataProvider to retrieve that data. This will work fine unless DataValidator needs one or more additional components. This is to to say that, if we are to add one more dependency in DataValidator then we will have to wire it up to the hierarchy which is DataProvider.

Imagine, that the service performing the validations now wants to log all the errors and for that it has a dependency on ILogger. In that case, if we are to add that dependency, the components will look like this:

 

 

Because the two components are tightly wired, the dependency on ILogger has to be injected up the hierarchy as:

var dataValidator = new DataValidator(this, new Logger());

As a result, the DataProvider has an additional responsibility of instantiating ILogger and providing it to DataValidator, while creating its instance.

 

The Solution

Since the two components need to talk to each other, there has to be a way to do so without having circular dependency. Surprisingly, there is a way – using EventHandlerThe proposal is, that DataValidator will raise the event “DataRequired”, prior to performing any data validation operation. DataProvider, on the other hand, will subscribe to this event and will respond by providing the data. 

The following code shows the use of EventHandler, in order to break the circular dependency:

 

 

Summary

To sum up, the DataValidator no longer has a dependency on DataProvider. It simply raises an event when it requires data, and any one who has subscribed to this event will respond.

This is one way of breaking circular dependency between components. Of course there may be a better solution, which I would love to learn about. So, do share!

Categories: Design Patterns & Principles

6 Comments

Steven Delashaw · October 27, 2018 at 7:41 pm

very interesting info ! .

behind · November 24, 2018 at 11:43 pm

Hey I know this is off topic but I was wondering if you knew of
any widgets I could add to my blog that automatically tweet my newest twitter updates.
I’ve been looking for a plug-in like this for quite
some time and was hoping maybe you would have some experience with something like this.

Please let me know if you run into anything. I truly enjoy reading your blog and I look
forward to your new updates.

    Gaurav Gahlot · November 25, 2018 at 9:31 pm

    Hey! If I understood your question correctly, I think you can use Custom Twitter Feeds. Do let me know if it solves your problem.

Emmitt Kadle · December 2, 2018 at 9:07 am

Throughout the awesome scheme of things you receive an A+ for effort. Where you actually lost us was on your specifics. As it is said, the devil is in the details… And that couldn’t be much more accurate here. Having said that, permit me say to you precisely what did do the job. Your writing is definitely extremely persuasive and that is most likely why I am taking an effort to opine. I do not really make it a regular habit of doing that. 2nd, whilst I can easily notice a jumps in reason you come up with, I am not necessarily confident of just how you appear to connect the details which help to make your final result. For right now I will, no doubt yield to your point however wish in the future you connect the dots much better.

Burton Shuffleburg · December 4, 2018 at 11:48 pm

glad to be one of several visitors on this awesome internet site : D.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.