c# interview question with answers :- What is circular dependency ?

c# interview question with answers :- What is circular dependency ?


In this video we will try to understand what exactly do you mean by Circular Dependencies in what kind of situation they raise and how can be resolve them so let’s first try to defined what exactly is circular dependency. circular dependency is situation where class library A is trying to use class library B and class library B is trying to use class library A you must be wondering that what kind of situation are those when class library would like to use each other so let me talk about very normal situation here which arises now and then now let consider this situation of a three layer architecture in a three layer architecture we have user interface then we have the Middle Tier then we have the Data Access Layer for now let’s concentrate on Middle Tier and the Data access layer so here is the simple solutions i have you cane see that i have very simple middle layer here in this middle layer i have this customer class and this customer class actually calls up the data access layer and in the data access layer i have the customer Dal so this customer class you can see that it has add method and the from add method it actually goes and creates an object of customer dal object and calls add method of the dal object and this is my data access layer what here very simple project here the middle tier which is the customer object the customer has add method the add method actually call the customer dal object he passes all the values that is customer code and customer name and then the dal object actually goes and add this to the database currently i have not put any code here for database let’s assume that this is probably field with entity framework code must be it is fill with simple data set or command object code whatever it is. and also if you see the solution Explorer here you can see that currently the middle tier is using the customer dal component it has to use the dal component or else you won’t able to create this object here of customer dal class isn’t it so you can see the middle tier is using the customer dal object in the customer dal object actually makes all the addition and deletion it has all the database code for doing the manipulation now we would like to go and improvise this code improvise this code means for example when the middle tier passes data to the data access layer you can see that it actually uses this variable to pass data for example you can see that the customer data access layer actually takes a variable entries like customer and customer name it would be great if i can just pass here start foreword the customer class like this isn’t it so that the complete customer object it goes as it is now this approaches much better why because to tomorrow let’s say that if you add new properties like let’s say phone number or pin code you don’t need to go and change your add input parameters it’s part of the customer object it comes as it is so your interface will not change to much but in order to implement this what we need to do is we need to reference the customer library into the data access layer in other words what we need to do is we need to go and add the reference of the middle tier to the customer data access layer because in the middle tier we have the customer object so if you see here the customer object is currently represent in the customer dal so if i have to reference this class over here i need to reference the middle tier but now if i try to do that let’s say if i try to go and add reference of the middle tier here you can see that it gives me error saying that this would cause a circulate dependency means what means your middle tier is already referencing customerDal so how can your customerDal now references middle tier that means you are saying that customerDal will reffer Middle tier and middle tier will refer CustomerDal which will lead to Circulate dependency so now the next question is how do we go and ensure that this customer object can be used inside customerDal and how do we ensure that this circulate dependency problem does not occur one of the proper way of ensuring that you don’t have circulate dependency is by using interfaces the two library which need to use each other they can go via interface in other words if i want rally get around this problem what i need to do is i to rally go and create a interface which actually mimic this customer class very quickly let me give the solution to this problem so we will go ahead and add a new class project or class library saying ICustomer this will have interface of customer so we will go ahead here and create a interface called as ICustomer now it does not look good that your interface name and the namespace name are same so what i will do is i will just go and name this as INSCustomer namespace for customer i know that the just naming conveniences bad but just ensure that the interface name and the namespace name are not same and this interface customer will have the same properties as the customer class it will have the same interface as a customer class so this interface will have customer code as well as will have customer name so let me just go and build this build succeeded and the next thing what will do is we will reference this ICustomer in this middle tier and we will implement this interface ICustomer on to the customer class so i will go and add a reference of this interface customer here and i will implement this interface here ICustomer first thing is we need to go and import that namespace INSCustomer and i will go and implement is interface here it has the set and get properties there it is we don’t even need to do is because already those properties name are already implemented here so i think this will just build up so you can now see that this customer actually implement this ICustomer the next thing what i will do is i will go to the data access layer and i will reference this ICustomer in the data access layer as well i am going to reference this ICustomer in the data access layer and i will say here using INSCustomer now here what i will says that in this add method i would like to get the reference of the customer object so i will say here ICusobj now from the middle tier what we can do is because this customer implements this ICustomer you can just go ahead and pass the current object over here saying this and what will happen is by using polymorphism or i will say by using dynamic polymorphism this customer object will get type casted to this ICustomer so you can now see what is happening is actually your customer data access layer is referencing by customer and the middle layer is also reference the ICustomer so circular dependency is removed now because both of this guys are going via this interface so you can now see that you will get strongly type object over here ICustObj. you will get a all the properties what you need and second this customer data access layer is not referencing the middle tier directly so even the circular dependency is not happening so one of the proper way of removing circulate dependency is by using interfaces so the two part is or two class library which wants to communicate to each others what they can do is they can create a interface like this and talk with each other with that both this class library don’t have to refer each other physically so the circular dependency does not happen and also they are able to communicate with each other why because they will going via this interface now the other way of removing circular dependency is by using events so other would have been that if you are middle tier can actually emit out events and your CustomerDal can subscribe to those events then again you can remove circular dependency but i always found that going via common interface is much structural way as compare to event because when we actually emit out event it is not strongly type for example here now you can see that i am getting a strong type customer object over here so just summarizing circular dependency is situation where one library wants to use the other library and the other library wants to reference to the first library and you can remove circular dependency by using a interface and between So hope that you are enjoying with this video in this video we will trying to understand what is circular dependency and what situation can arise and how can we resolve it. THANK YOU SO MUCH :- 🙂

15 thoughts on “c# interview question with answers :- What is circular dependency ?

Leave a Reply

Leave a Reply

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