Sunday, 1 December 2013

Dependency Injection Design Pattern

Dependency Resolution

Dependencies can be injected at compile time (hard-coded), deployment time (configuration based or dependency injection container) at run-time (specified from the UI). Technically, dependencies can be specified in the constructors, property setters and methods. Containing class can have a data member to hold the Strategy object. But .NET provide Funcs/Anonymous  delegates or lambda expressions to refer the method/behavior/strategy that can be passed to class at runtime.

Usually constructors take the arguments while creating the object, these input are the dependencies of the 
object those needed to be full filled before we can get the intended functionality of the object. All dependencies must be specified before invoking the intended functionality of the object.

Alternatively put, software loaded in the RAM is nothing but an object graph loaded in the memory that 
gives us intended functionality. This object graph is composed by different type of relationships among the objects. This web of objects loaded in the memory in the form of object graph is important. This object graph can extend and new objects become the part of object graph as the software is executed and different objects are initialised. Lazy loading of objects is also related to this.

Dependency Injection allow us to compose this object graph dynamically based on the XML files. So the objects are not wired or composed in a hard manner. They are wired and composed based on the XML configuration files. Dependency Injection containers are the composition engines those read the XML configuration files and compose the files accordingly.

Structure Map, Windstle Caster, MEF Managed Extensibility Framework, Unity, are the examples of DI Containers.

Dependency can specified in XML configuration files those can be loaded by DI container or by the Configuration mechnims provided by the development platform. 

Depency can be specifed the GUI control, user select the type of dependency that is needed to be injected.

No comments:

Post a Comment