AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Tuesday 27 November 2018

Inversion of Control and how it's different from the dependency inversion principle?

Most of the people mix the DIP, IOC and DI together but, Inversion of Control is part of a dependency inversion principle.  As per  dependency inversion principle high level module should not be dependent on lower level modules so Inversion of Control is a way to solve this problem.

If we fitting everything together it will be looks as below image.


Inversion of Control can be achieve by three ways

  • Interface Inversion
  • Flow inversion
  • Creation Inversion

Interface Inversion
Interface should have more than one implementation.

Flow Inversion
Control of the code flow is totally inverted.
let's take an example of a command line program where  program is asking for inputs one by one.
  • What's your name
  • Shivam
  • Whats your favorite color?
  • Blue
Now I inverted the code flow to GUI and user is controlling the code flow.
Creation Inversion
Creation Inversion is creating a object outside of the class they are being going to use. Factory pattern is the example of creation inversion.

Types of creation inversion

  • Factory Pattern
    Button button = ButtonFactory.CreateButton()
  • Service Locator
    Button button = ServiceLocator.Create(IButton.class)
  • Dependency Injection
    Button button = GettheRightButton()
    OutScreen ourscreen = new OutScreen(button);

0 comments :

Post a Comment

  • Popular Posts
  • Comments