Inversion of Control(IoC) is a design mechanism to decouple components dependencies, a light-weight implementation is: TinyIoC, which is also part of Nancy.
IoC idea uses commonly in webUI(and backend server) apps, which is an user friendly solution to cloud deployment management as well as apps in mobile, which should be the right direction in future ADS software tool development.
the idea of IoC can be explained by the following example from register and resolve in unity container
|
|
the Driver
class depends on ICar
interface. so when instantiate the Driver
class object, need to pass an instance of ICar
, e.g. BMW, Ford as following:
|
|
- Register
create an object of the BMW
class and inject it through a constructor whenever you need to inject an ojbect of ICar
.
|
|
- Resolve
Resolve
will create an object of the Driver
class by automatically creating and njecting a BMW
object in it, since previously register BMW
type with ICar
.
Driver drv = container.Resolve<Driver>();
drv.RunCar()
summary
there are two obvious advantages with IoC.
the instantiate of dependent class can be done in run time, rather during compile. e.g.
ICar
class doesn’t instantiate inDriver
definitionautomatic
new class
management in back.
the power of IoC will be scaled, once the main app is depends on many little services.