Spring.NET is an open
source application framework that makes building enterprise .NET applications
easier. Spring framework includes several modules that provide wide range of
service like Testing, Messaging, Authentication, MVC, Data access, Aspect
oriented programming..Etc. It offers a lightweight Dependency Injection
container built around the concept of the factories Design pattern. Spring
is a framework that helps you to "wire" different components together
and lets you connect components, label them, control their lifecycle/caching,
and alter behavior based on configuration.
This sample is pretty
simple, from a POC perspective which attempts to demonstrate feature of spring
.NET. We had a Document Abstraction engine which enable client to switch
between different services based on the license that client holds. The idea
here was to build this application using spring.net concept.
The below sample
application demonstrate how to achieve dependency injection using Spring.Net. We
are going through following steps:
1. Define Interface
1. Define Interface
2. Class
implementing interface
3. Set
up a config file.
4. A console application to utilize spring.net feature.
4. A console application to utilize spring.net feature.
Step1: Define Interface
public interface
IDALService
{
string FilePath
{
get;
}
}
Step2: Class implementing
interface
The code in Listing 2
contains two classes, SPSService and WSSService which each implement the interface.
In these Implementation Classes, there is a FilePath property which simply
returns the path of the file, depending
on which concrete class is used. This Interface and implementation classes will
be used to demonstrate the advantage of using the Spring .NET implementation.
public class
SPSService : IDALService
{
public SPSService() { }
public string
FilePath
{
get
{
return "SharePoint
Portal Services (SPS)initialized ";
}
}
}
public class
WSSService : IDALService
{
public WSSService() { }
public string
FilePath
{
get
{
return "Windows
SharePoint Service (WSS) initialized ";
}
}
}
Step3: Set up a
config file.
Now, add the config.xml file, which drives the Dependency
Injection. The configuration file consists of only two elements, which contain all of the object definitions.The object definition consists of three basic attributes which define what object is created and how it's created.The name attribute defines the name that the Windows Application class uses when requesting an object from the config.xml file. In this case, it is "WSS". This name is just used to reference the definitions contained in the configuration file from the client code.Next is Singleton, Spring.NET has built-in support for making objects singletons.Finally, the type attribute defines the actual type of the object to be created. This object type will actually be loaded and returned when the config.xml file is queried. This string will be in the form of "Type, Assembly", wherein Type indicates the type of object and the Assembly indicates the Assembly Name which contains the object type. As indicated in the configuration file, the type desired is one of the types shown in step 2
Step4: A Console
application to consume.
Here is the Windows
Console application project from where we call the above code.
using System;
using System.Text;
using Spring.Objects.Factory.Xml; (include dll)
using SpringSampleDLL;
using Spring.Core.IO;
using Spring.Objects.Factory.Config; (include dll)
namespace SpringSampleClient
{
class Program
{
static void Main(string[] args)
{
string resourceName =
"Config.xml";
XmlObjectFactory xmlObjectFactory = new XmlObjectFactory(new
FileSystemResource(resourceName));
IDALService objDALInterfaceWSS =
(IDALService)xmlObjectFactory.GetObject("WSS");
IDALService objDALInterfaceSPS =
(IDALService)xmlObjectFactory.GetObject("SPS");
Console.WriteLine(objDALInterfaceWSS.FilePath);
Console.WriteLine(objDALInterfaceSPS.FilePath);
Console.ReadLine();
}
}
}
Here you see, by just changing the type of the implementation class in the
configuration file from "WSS" to "SPS", we can dynamically
alter the class that is returned to the client, all without a recompile. This
sample code demonstrated how to utilize Dependency Injection features
with spring.NET. The Dependency Injection pattern simplifies application code,
and increases configuration flexibility by deferring component configuration
and assembly to the container.
Thanks for visiting this page, for any feedbacks and questions
please add a comment and I will be defiantly giving my response asap.
No comments:
Post a Comment