Tuesday 29 May 2018

Inversion of Control (IoC) in C#

Hi All,

I hope you are doing well and good.

This article demonstrates that what is "Inversion of Control(Ioc)" and How to use it in real time applications.


Inversion of Control(IoC):
  • The term Inversion of Control (IOC) refers to a programming style where a framework or runtime, controls the program flow.
  • Inversion of control means we are changing the control from normal way.
  • It works on Dependency Inversion Principle. Here "Dependency Inversion Principle(DIP)" helps us to develop loosely couple code by ensuring that high-level modules depend on abstractions rather than concrete implementations of lower-level modules. The Inversion of Control pattern is an implementation of this principle.
  • The most software developed on the .NET Framework uses IoC.

Applying Inversion of Control:

Following UML diagram for our example.



Now start coding for the above scenario.

  • Create an Interface Named “IVehicle” with 2 methods like below.

    public interface IVehicle
    {
        void Accelerate();
        void Brake();
    }
  • Create classes with names “Truck” and “Car” and implement them with above interface like below.

    public class Car : IVehicle
    {
        #region IVehicle Members
        public void Accelerate()
        {
            Console.WriteLine("Car accelerates...");
        }
        public void Brake()
        {
            Console.WriteLine("Car stopped.");
        }
        #endregion
    }
    public class Truck : IVehicle
    {
        #region IVehicle Members
        public void Accelerate()
        {
            Console.WriteLine("Truck accelerates...");
        }
        public void Brake()
        {
            Console.WriteLine("Truck stopped.");
        }
        #endregion
    }
  • Finally, create a controller class called “VehicleController” with parameterized constructor and responsible to create objects by using interface IVehicle. The code will be.

    public class VehicleController
    {
        IVehicle m_Vehicle;
        public VehicleController(IVehicle vehicle)
        {
            this.m_Vehicle = vehicle;
        }

        public void Accelerate()
        {
            m_Vehicle.Accelerate();
        }

        public void Brake()
        {
            m_Vehicle.Brake();
        }
    }
  • Now, let’s use the above code (IoC implemented code) from main method like below.

    class Program
    {
        static void Main(string[] args)
        {
            IVehicle vehicle = new Car();
            //IVehicle vehicle = new Truck();
            VehicleController vehicleController = new VehicleController(vehicle);
            vehicle.Accelerate();
            vehicle.Brake();
            Console.Read();
        }
    }

Now, you can run the application.

If you have any doubts in this post, please post in comments section.

Thanks for reading the post.

No comments:

Post a Comment

Intoduction to Flutter

Hi All, I hope every one is doing good In this article, we will know about the introduction of Flutter.