简体   繁体   中英

Dependency Injection : Using Constructor Injection

Yesterday, One of my friends asked me to create a program(Must implement Dependency Injection) which return speed of the car. So, I have created a small program in which I tired to implement constructor injection.

 class Program
{
    static void Main(string[] args)
    {
        Maruti objMaruti = new Maruti();
        clientClass obj = new clientClass(objMaruti);
        obj.getSpeed();
    }
}

public class clientClass
{
    ISpeed _ISpeed;
    public clientClass(ISpeed obj)
    {
        this._ISpeed = obj;
    }

    public int getSpeed()
    {
        return _ISpeed.Speed();
    }

}

public interface ISpeed
{
    int Speed();
}

public class Maruti : ISpeed
{
    public int Speed()
    {
        return 200;
    }
}
public class Audi : ISpeed
{
    public int Speed()
    {
        return 400;
    }
}
public class BMW : ISpeed
{
    public int Speed()
    {
        return 600;
    }
}

Now, In main Method, my friend can check speed of any car.

 class Program
{
    static void Main(string[] args)
    {
        Maruti objMaruti = new Maruti();
        clientClass obj = new clientClass(objMaruti);
        obj.getSpeed();
    }
}

He asked me a question that why you have created this constructor Injection and client class. If you directly call the class, you would get the result.

 class Program
{
    static void Main(string[] args)
    {
        Maruti objMaruti = new Maruti();
        objMaruti.Speed();

        //If user wants to check Audi Speed.
        Audi objAudi = new Audi();
        objAudi.Speed();
    }
}

Is he right? which way is best and why ?

I think the poor naming made your friend to ask you this question. What if you had the requirement to log the speed of different cars? The client class would have this name

public class SpeedLog
{
   ISpeed speed;
   ILogger logger;
   public SpeedLog(ISpeed speed, ILogger logger)
   {
       this.speed = speed;
       this.logger = logger;
   }
   public void Information()
   {
       logger.Information($"{speed.GetType().Name} is running with {speed.Speed()}");
   }
}

This client class can log the speed of any object as long as the object has the Speed method in its public interface. The ISpeed interface gives us the opportunity to define the signature of the Speed method. Now the SpeedLog type doesn't depend on a particular type like Maruti or Audi or even a base type Car , if any, to retrieve the speed info. SpeedLog can now log the speed of stars if the Star type implements the ISpeed interface. When we add the new Star type we will only need to compile the library which contains this type and the library of the SpeedLog type remains untouched. An Inversion of Control library can now inject the Star object if it is asked to do so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM