Spring.NET Constructor Injection Example

The following Console Application (C#) represents Constructor injection using Spring.NET. To use the code in as it is form, create a console application using C# in Visual Studio 2012 and name it Spring4. Copy the content of program file from below. Create another xml file named context.xml and add it to your project. Copy the contents of that file from what I have supplied. Last but not the least, download spring.net core from NUGET. You only need the Spring.net core, you can ignore the rest.

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spring;
using Spring.Context.Support;

namespace Spring4
{
    class Program
    {
        /// <summary>
        /// This program demonstrates constructor injection using Spring.NET
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            var context = new XmlApplicationContext("../../context.xml");

            Writer wall = (Writer)context.GetObject("writer");
            wall.WriteMessage();

        }
    }

    public class WriteMessage
        {
            WriteMessage(IWritingDevice device)
            {
                Console.Write("I am writing with " + device.DeviceName());
            }
        }

        public class Writer
        {
            private IWritingDevice _device;
            Writer(IWritingDevice device)
            {
                _device = device;
            }

            public void WriteMessage()
            {
                Console.Write("I am writing with " + _device.DeviceName());
            }
        }

        interface IWritingDevice
        {
            string DeviceName();
        }

        public class Pen :IWritingDevice
        {
            public string DeviceName()
            {
                return "Fountain Pen";
            }
        }

        public class Pencil :IWritingDevice
        {
            public string DeviceName()
            {
                return "Grey Pencil";
            }
        }
}

Context.xml

<?xml version="1.0" encoding="utf-8" ?>


  
    
  

  <object id="Pen" type="Spring4.Pen, Spring4" />
  <object id="Pencil" type="Spring4.Pencil, Spring4" />
</objects>

Explanation

In this example, I am using a writer Object to write something. But the writer object cannot write by itself. It needs a writing device to write with. It is required that is supplied the object in the constructor.

My Writer Object is dependent on writing device, which must implement IWritingDevice. We can pass anything to this class which is derived from IWritingDevice interface. In the given example, I trying to write with fountain Pen.

Fountain Pen or Simply Pen is passed to the constructor of Writer Object. This is configured in context.xml. Notice that you cannot just change the constructor argument on the fly, it is configured in XML file in Spring context and will always be the same.

Notice that no new kewyord is used. When Writer Object is created, Spring automatically knows that a IWritingDevice object is needed for Writer Object so it it can write with something.

Some useful links

Type of dependencies

Advertisements
This entry was posted in Software Development and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s