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 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.
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.
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:
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.
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:
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.
Define your interfaces that will be exposed by the layers.
Provide concrete implementation for the interfaces defined by your layer.
Configure the objects in the Spring.NET configuration.
Initialize the Spring.NET configuration at runtime using the Spring.NET API.
Use the Spring factory to create the instance for you.
Consume the instance created by the Spring factory in the calling layer.
Having seen the steps at 10,000 feet, it's now time to see how things are implemented at ground level.
I have created a layered application with three projects. Each layer defines the interface and the implementation.
The Data Access Layer defines the interface, which the BLL will refer to. This interface defines methods that will retrieve data from a table.
public interface IDAL
{
DataTable GetAll();
DataTable GetById(string ID);
}
The GetAll() and GetById() methods are shown as below:
public class CustomerDAL : IDAL
{
// Implementation for GetAll()
public DataTable GetAll()
{
// Create connection
// Create command
// Retrieve all records and return as DataTable
}
// Implementation for GetById()
public DataTable GetById(string ID)
{
// Create connection
// Create command with filter criteria
// Retrieve matched records and return as DataTable
}
}
So far so good. Now, it's time for us to understand the Spring configuration. First, you declare the Spring configuration section, then define the object/object node with type and assembly information. The object node's name attribute must be unique, and it uniquely identifies the object. The type specifies the full type of the object. The singleton attribute specifies whether to create a single instance and serve for multiple requests (singleton implementation), or a new instance always.
The following snapshot gives the configuration of the application:
Add a reference to the Spring.Core assembly. Include a reference to the Spring.Context and Spring.Context.Support namespaces.
Initialize the configuration using the ContextRegistry.GetContext() method. This will validate the configuration, check for the existence of objects, and return an IApplicationContext instance.
Use the IApplicationContext instance returned by the ContextRegistry.GetContext() method to get the instance.
// Include namespaces
using Spring.Context;
using Spring.Context.Support;
// Get the context
IApplicationContext applicationContext = ContextRegistry.GetContext();
// Get the instance through Spring configuration
IBLL _customerBLL = applicationContext[“customerBLL”];
// Work with the instance
_customerBLL.GetAll();
Once the object is instantiated, you can go ahead and perform the operations that you wish to. In our case, Customer DAL's GetAll() method is called from the Customer BLL instance.
After working through Spring.NET DI, one obvious question is how it can be helpful in a real time scenario. Following are some of cases where DI can be used in a real time basis:
The sample application has two forms - Customer and Employee. Both retrieve all data or data based on an ID. They follow a similar pattern.
When the form gets loaded, the presenters are initialized using Spring.NET's DI technique to the appropriate presenter. The Customer form gets linked to CustomerPresenter; similarly, the Employee form gets linked to the EmployeePresenter instance. The presenter gets linked to the appropriate BLL layer. The BLL layer gets linked to the appropriate DAL instance.
All these happen using Spring.NET's DI technique.
Dependency Injection is a key technique which helps in building loosely coupled systems. Spring.NET offers it as an out of the box solution, and it is the base principle for the rest of the components within Spring.NET.
Happy reading! Happy coding!
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||