Inversion of Control ( IOC ) - Framework vs. Library

 Inversion of control (IoC) or Dependency injection (DI) is one of the feature of a framework. When framework provides you interfaces to extend, so that framework can execute your code to complete certain operation, is called inversion of a control. 

For example, when you register a api route to a controller class, you are configuring the framework, so that framework can execute a configured route method.


Dependency injection is an example of IoC, where framework construct the instance of a given interface or class and inject to a given controller or service. In this example too, control is in a framework side.

All of the framework has DI feature, and so IoC.

In asp.net api, you implement ApiController class and use route related prefix in the controller method, so that when request comes with the matching pattern, framework knows which method to invoke.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0 


In Java Spring Framework too we configure route and use dependency injection. The instance of a class or service that spring dependency container manages are called spring beans or java beans.

https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/beans.html 


In angularjs also we configure service like below, so that angularjs framework can create the new instance of a service with service id "serviceId" and can provide it to another service or controller. So angularjs is a framework.

var myModule = angular.module('myModule', []);
myModule.factory('serviceId', function() {
  var shinyNewServiceInstance;
  // factory function body that constructs shinyNewServiceInstance
  return shinyNewServiceInstance;
});


On the other hand, React is a javascript library because it is missing IoC or DI feature for a framework. It is also sometimes called as a framework because the react library code manage the life cycle of a component that we create. For example, react library calls componentDidMount method of a comonent that we create by extending the Reac.Component class. 

https://reactjs.org/ 

Comments