patterncsharpMinor
Dependency Inversion Principle and Dependency Injection in C#
Viewed 0 times
principledependencyinjectionandinversion
Problem
I was doing some sample example which were perfectly fine. But, when I read about SOLID design principle, I had to change my code.
I have implemented DIP and DI (Constructor Injection) in my code. Please let me know whether I have implemented successfully or not.
Old code before SOLID design principlss:
Newly modified code:
```
class Program
{
static void Main(string[] args)
{
//Now, It depends on my requirement to call any sex class.Let's Say, I want to call Female Class.
lowerClass_FeMale objLowerfemale = new lowerClass_FeMale();
Higherclass obj = new Higherclass(objLowerfemale);
obj.CallLowerClass();
Console.ReadLine();
}
}
public interface ISex
{
void DoSomeAction();
}
public class lowerClass_Male : ISex
{
public void DoSomeAction()
{
Console.WriteLine("Hey It's Male. Do Some Action");
}
}
public class lowerClass_FeMale : ISex
{
public void DoSomeAction()
{
I have implemented DIP and DI (Constructor Injection) in my code. Please let me know whether I have implemented successfully or not.
Old code before SOLID design principlss:
class Program
{
static void Main(string[] args)
{
Higherclass obj = new Higherclass("Female");
obj.CallLowerClass();
Console.ReadLine();
}
}
public class Higherclass
{
public readonly string sex;
lowerClass_Male objLowerClass_Male = null;
lowerClass_Female objLowerClass_Female = null;
public Higherclass(string _sex)
{
sex = _sex;
}
public void CallLowerClass()
{
if (sex == "Male")
{
objLowerClass_Male = new lowerClass_Male();
objLowerClass_Male.DoSomeAction();
}
else
{
objLowerClass_Female = new lowerClass_Female();
objLowerClass_Female.DoSomeAction();
}
}
}
public class lowerClass_Male
{
public void DoSomeAction()
{
Console.WriteLine("Hey It's Male. Do Some Action");
}
}
public class lowerClass_Female
{
public void DoSomeAction()
{
Console.WriteLine("Hey It's FeMale. Do Some Action");
}
}Newly modified code:
```
class Program
{
static void Main(string[] args)
{
//Now, It depends on my requirement to call any sex class.Let's Say, I want to call Female Class.
lowerClass_FeMale objLowerfemale = new lowerClass_FeMale();
Higherclass obj = new Higherclass(objLowerfemale);
obj.CallLowerClass();
Console.ReadLine();
}
}
public interface ISex
{
void DoSomeAction();
}
public class lowerClass_Male : ISex
{
public void DoSomeAction()
{
Console.WriteLine("Hey It's Male. Do Some Action");
}
}
public class lowerClass_FeMale : ISex
{
public void DoSomeAction()
{
Solution
The new code seems OK to me.
One way to know if you're on the right way is trying to unit test your code. In the old code you couldn't unit test the higher class, as you needed to create real instances of the lower classes inside the code, so you needed to test both the higher and the lower classes at the same time.
In the new code you can test the lower classes and the higher class separately, as now you can inject a mock of the
Note that you have two options to inject the lower class. If the lower class is only going to help in one method of the higher class, maybe it will be better to inject the lower class as a parameter of that method. If the lower class is going to help all along the higher class, then pass it as a parameter of the constructor, as you have it now.
One way to know if you're on the right way is trying to unit test your code. In the old code you couldn't unit test the higher class, as you needed to create real instances of the lower classes inside the code, so you needed to test both the higher and the lower classes at the same time.
In the new code you can test the lower classes and the higher class separately, as now you can inject a mock of the
ISex interface into the higher class and test that the proper methods of the lower classes are invoked.Note that you have two options to inject the lower class. If the lower class is only going to help in one method of the higher class, maybe it will be better to inject the lower class as a parameter of that method. If the lower class is going to help all along the higher class, then pass it as a parameter of the constructor, as you have it now.
Context
StackExchange Code Review Q#141068, answer score: 5
Revisions (0)
No revisions yet.