Monday 22 June 2015

Dependency Injection in Spring.Net

Spring.net developers in India

Dependency Injection is a technique that provides decoupling application from the actual implementation during design/compile time and link them at run time. 

When we consume a class instance like ClassB from another class instance ClassA at compile time, then any changes which going to be made to ClassB will affect ClassA. For any single change, we need to recompile, redeploy the whole thing. Here, there is a static or compile time binding between ClassA and ClassB. Dependency Injection is one of the techniques that relieves us from the pain of static binding and decouples ClassA and ClassB at runtime. The end result, a decoupled or loosely coupled system.

Let’s consider an application being developed by software development companies which contains three layer Presentation Layer (UI), Business Login (BAL) Layer and Data Access Layer (DAL).  In that UI interacts with BAL and BAL interact with DAL. In tightly coupled system, UI is depended on BAL and BAL is depended on DAL so problem is that if make any changes in DAL then it is required to compile or redeploy rest of the layer. Where in case of loosely coupled system any instance can be changed at runtime rather than compile time. So by enforcing DI in application development we can link the layer at runtime, we can also develop layer independently and integrate later.  

Advantage of Using DI:

  • Consider following for DI, whichcan be used in a real time:
  • The dependent layer can be changed at runtime based on the environment.  
  • The rapidly bug fixes can be done offline and linked at integration/testing phase 
  • Side by side development of different layers which can be linked at integration phase.

Creating loosely typed System Using Dependency Injection

At first, create layered architecture in which there are Presentation, Business login and Data Access layer. UI interacts with Business Login and Business Login further interact with Data Access layer to get data.

Here by adding dependency injection in layered architecture, we can get change any instance at runtime rather than at compile time so there will not any dependency among all layers. Moreover, we are going to link layer at runtime.

Steps to implement DI in DotNet:

  1. At first, it is required to create Interfacefor all layers, especially interface for Business login and Data access layer which contain method definition.
  2. Give implementation of interface by both layers, BAL and DAL respectively.
  3. Perform Spring configuration in dotnet in configuration file in which create unique instant for all above layer so that they can serve multiple request.
  4. Add reference for Spring.Core in application and initialize your configuration by ContextRegistry.GetContext() method which validates about existence of your objects that you have created in configuration file and this method will return IApplicationContext instance
  5. Now once instance is initialized then you can call method of DAL from BAL instance.

Real time use:

  • Runtime change the dependent layer like create two kind of environment one for production which contains actual implementation and another for testing environment.
  • Parallel development of layer and final can be integrated or linked.
  • decoupling application from the actual implementation during design/compile time and link them at run time is the prime use of dependency injection for custom software development companies


No comments:

Post a Comment