Dependency Injection using Spring.NET

/

Introduction
Spring.NET is one of the popular open source frameworks ported from Java platform. It offers a lot of functionality, and in this article, I will discuss about Dependency Injection and how it is supported in Spring.NET.
Dependency Injection
Dependency Injection is a technique that decouples the consumer from the actual implementation during design/compile time and binds them at run time.
At a lower level of implementation, in a typical application, there will be a class that implements a logic, and it will be consumed by another class. The reason why we have two classes will drag us to discussions like modularization of code, reusability, maintainability etc.
When we consume a class instance (let's say, ClassB) from another class instance (ClassA) at compile time, then any changes to ClassB will affect ClassA. For every change, we need to recompile, redeploy the whole stuff, and other related issues. In this case, 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. The end result, a decoupled or loosely coupled system.
There are lot of materials available to understand DI in detail. Martin Flower has an excellent article that explains the concepts, with examples.
DI using Spring.NET
Spring.NET offers DI as an out of the box solution. In this article, I will dig deep and explain how we can leverage the benefits offered by Spring.NET.
Scenario
I created an application that has the typical three layers - Presentation, Business Logic Layer (BLL), Data Access Layer (DAL). The UI layer implements the MVP pattern, in which I created views (WinForms), Presenter classes, and DataTable as Models.
The UI calls the Presenter, which interacts with BLL; BLL interacts with DAL and retrieves data from the Northwind database.
The following diagram shows the layers of the application:
Limitations of a tightly coupled system
In a tightly coupled system, the UI is dependant on BLL, and the BLL is dependant on the DAL. If we change something in the DAL, then all the layers need to be recompiled and redeployed.
Advantages of a loosely coupled system
Instead, if we develop in a loosely coupled manner, then the compile time dependency can be avoided. The instances can be chained at runtime. This gives the following advantages:
Decouples the layers
Ability to link the layers at runtime
Ability to link a different implementation at runtime for QA/testing purposes
Ability to develop the layers independently, and integrate at a later point
Steps to implement DI using Spring.NET
Having discussed about the advantages, it's now time to see how we can go about implementing the technique. Following are the broad steps that need to be followed to implement DI using Spring.NET.
Step 1
Define your interfaces that will be exposed by the layers.
Step 2
Provide concrete implementation for the interfaces defined by your layer.
Step 3
Configure the objects in the Spring.NET configuration.
Step 4
Initialize the Spring.NET configuration at runtime using the Spring.NET API.
Step 5
Use the Spring factory to create the instance for you.
Step 6
Consume the instance created by the Spring factory in the calling layer.

0 comments:

Post a Comment

Powered by web analytics software.