I love the idea of Dependency Injection (DI) but I always run into a wall when I try to use DI in a real world example. While processing within an instance of a class (MyParentClass) I often want to pass any "Special" processing to an instance of a class (MyChildClass). Often I want to use a DI instance(MyStringManipulator) within that child class. The only working way I have found to accomplish this is by passing that DI instance (MyStringManipulator) through the whole chain of parent->child instances within my class library. Seems like there should be an easier/better way. Thank You for your help.
public class MyParentClass
{
public string ManipulateString( string s)
{
MyChildClass my = new MyChildClass();
return my.Process(s);
}
}
public class MyChildClass
{
MyStringManipulator _manipulator = null;
public MyChildClass (MyStringManipulator manipulator )
{
_manipulator = manipulator;
}
public string Process( string s)
{
return _manipulator.Manipulate(s);
}
}