July 29, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Dependency Injection with Spring.Net

  • January 22, 2008
  • By David Consdorf
  • Send Email »
  • More Articles »

Dependency Injection with Spring.Net

So, where does Spring.Net fit in? Well, the core of the Spring.Net framework is built on top of the idea of factories and Dependency Injection. Spring.Net is basically just one big configurable object factory. Instead of having to hard-code the often complex factory logic, you simply can provide Spring.Net an XML configuration file to "wire" your objects together.

I could go on, but I have already talked enough about theory. Instead, look at some examples of Spring.Net in action. For this article, I will introduce you to the framework and show you a small sample web application that touches on several of the key Dependency Injection principles of Spring.Net. Note that there are many options for Dependency Injection in Spring.Net and this sample is far from a complete example of everything Spring.Net can do, but it is a good start.

To download the example code in this article, click here: Download Code Examples.

Configuring Spring.Net

Start by adding Spring.Net to your project. First, download the spring.Net library at www.springframework.net and add the assemblies to you project. You should only need the core assembly for this example but the others can be added too.

Next, add the following code to your Web.config or App.config file:

<configSections>
   <sectionGroup name="spring">
      <section name="context"
               type="Spring.Context.Support.ContextHandler,
                     Spring.Core" />
      <section name="objects"
               type="Spring.Context.Support.DefaultSectionHandler,
                     Spring.Core" />
   </sectionGroup>
</configSections>

<spring>
   <context>
      <resource uri="config://spring/objects" />
   </context>
   <objects xmlns="http://www.springframework.net">
      <!—- ToDo: Put mappings here. -->
   </objects>
</spring>

Listing 1: Spring.Net Configuration

This is just the basic setup configuration for Spring.Net. There isn't much to it really. At this point, you can start adding objects definitions inside the objects tag.

A Simple Message Object

Start with a simple message object. This example will show you how to create an object definition where the application retrieves an object with a simple string injected into it by Spring.Net.

First, create an object definition and interface as such:

public interface ISimpleMessage {
   String Message { get; }
}

public class SimpleMessage : ISimpleMessage {

   private String _message;

   public SimpleMessage() {
   }

   public SimpleMessage(String message) {
      _message = message;
   }

   public String Message {
      get {
         return _message;
      }
      set {
         _message = value;
      }
   }
}

Listing 2: Simple Message Object

Notice that the interface only provides a method for reading the message and not changing it. This is because the message is expected to be set already when any object reads the message though the interface.

Next, make a mapping for the object in your Spring.Net configuration:

<objects xmlns="http://www.springframework.net">

<!-- Messages -->
   <object name="SimpleMessage"
           type="SimpleMessage, __Code"
           singleton="false">
      <constructor-arg name="message"
         value="Hello, I am a simple message set by using
         constructor-based dependency injection." />
   </object>
</objects>

Listing 3: Simple Message Object Mapping

What you did here is make a definition for constructing this object. The name of the object is the handle that will be used to retrieve this object. The class simply defines the class of the object instance. (Note that "__Code" is just the generic namespace for web apps. In non-web apps and when referencing assemblies of objects, use the full namespace instead.) Also, note the "singleton" property is set to false in this example. You will learn what this means and more about the singleton pattern later in this article.

Finally, to set the message property of the object, you set a message value as a constructor argument for the object. So now, when the object is initialized, the message will be initialized in the constructor.

Now, run some code to retrieve the object from the Spring.Net context:

// Get Spring.Net context
using (IApplicationContext ctx = ContextRegistry.GetContext()) {

   // Get a new instance of the message object
   ISimpleMessage simpleMessage1 =
      (ISimpleMessage)ctx.GetObject("SimpleMessage");

   // Read the message objects message as initialized by Spring.Net
   ltlSimpleMessage1.Text = simpleMessage1.Message;
}

Listing 4: Retrieve Simple Message from Spring.Net Context

It is pretty easy, right? All this code does is retrieve the Spring.Net application context and then ask the context for an instance of the "SimpleMessage" object. This piece of code only knows the name of the object and what interface to use. It does not know what concrete class is implemented by the interface and it does not have to worry about how to inject the correct message into the object because Spring.Net handles it.





Page 2 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel