I am new to Dependency injection Pattern. Please look in to the below scenario. Right now, my below code is in tightly coupled. I want to make this as lightly coupled.
Could someone help me out to implement dependency injection using Unity?
// Implementation of class A
public class A
{
public B b{get;set;}
public A(B b,string c)
{
this.b=b;
this.A(b,c);
}
}
//Implementation of Class B
public class B
{
public int value1 {get;private set;}
public string stringValue {get;private set;}
public B(int value,string strValue)
{
this.value1=value;
this.stringValue=strValue;
}
}
//Implementation of class C
public class C
{
public void Dosomething()
{
B b=null;
string value="Something";
// Here I need to implement unity to resolve tight coupling
// without creating object of Class A
A a=new A(b,value);
}
}
I tired something based on the blow possible duplicate question. But still I am facing same issue.
How should I register this type that has parameterized constructor, in Unity?
Instead of this line, I have implemented the below code
A a=new A(b,value);
var container=new UnityContainer();
container.RegisterType<A>();
container.RegisterType<B>();
A a=new A(b,container.ResolveType<B>();
But its not helping me out.