Inject Dependency with Spring.NET and Generics

In my first post about Dependency Injection with Spring.NET I provided a simple example and explanation of how to use Spring.NET.  Having fiddled with Spring.NET further I quickly realized I was duplicating code to load configured objects.  I thought my method may be useful to others.

using System.IO; 

using Spring.Objects.Factory.Xml;

 

namespace TestApp

{

    public class ConfigHelper

    {

        public static T GetInjectedObject<T>(string configFileLocation, string configObjectName)

        {

            using (Stream stream = File.OpenRead(configFileLocation))

            {

                Spring.Core.IO.InputStreamResource resource = new Spring.Core.IO.InputStreamResource(stream, "config");

                XmlObjectFactory xmlObjectFactory = new XmlObjectFactory(resource);

                return (T)xmlObjectFactory.GetObject(configObjectName);

            }

        }

    }

}

For this method to work your project must reference Spring.Core.dll.  To call this method supply the location of the configuration file and the name of the configuration key to be used, which represents the type of object to be created.

The above method is very basic but not quite the efficient implementation one would want in a production app.  As an update to this post I've included a more efficient method which holds XmlObjectFactory objects in a static Dictionary (thanks for the comment Brian).  The first call to this method for a particular config file will load it and place it's XmlObjectFactory in the static Dictionary.  Subsequent calls will use the object from the Dictionary.  Some may prefer to use cache so objects not used frequently are disposed of.  I didn't just to stay more compatible with a variety of implementations.

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.IO;

 

using Spring.Core.IO;

using Spring.Objects.Factory.Xml;

 

namespace TestApp

{

    public class ConfigHelper

    {

        /// <summary>Used for safe manipulation of objects.</summary>

        private static object _lockObject = new object();

 

        /// <summary>Holds previously acquired XmlObjectFactory objects.</summary>

        private static Dictionary<string, XmlObjectFactory> _objectFactories =

            new Dictionary<string, XmlObjectFactory>();

 

        public static T GetInjectedObject<T>(string configFileLocation, string configObjectName)

        {

            XmlObjectFactory objectFactory = null;

 

            if (_objectFactories.ContainsKey(configFileLocation))

            {

                objectFactory = _objectFactories[configFileLocation];

                Trace.WriteLine(string.Format("Object factory found in Dictionary - {0}, {1}", configObjectName, configFileLocation));

            }

 

            if (objectFactory == null)

            {

                //load the configuration from disk

                using (Stream stream = File.OpenRead(configFileLocation))

                {

                    InputStreamResource resource = new InputStreamResource(stream, "config");

                    objectFactory = new XmlObjectFactory(resource);

 

                    //double-check lock so we don't attempt to add twice

                    lock (_lockObject)

                    {

                        if (!_objectFactories.ContainsKey(configFileLocation))

                        {

                            _objectFactories.Add(configFileLocation, objectFactory);

                            Trace.WriteLine(string.Format("Object factory added to Dictionary - {0}, {1}", configObjectName, configFileLocation));

                        }

                    }

                }

            }

 

            return (T)objectFactory.GetObject(configObjectName);

        }

    }

}

Surely this is just one step in the evolution of working with Dependency Injection.  If you've some pointers I haven't considered please share! 

kick it on DotNetKicks.com
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Related posts

Comments

March 3. 2008 02:28 PM

Brian

I could be wrong about this, but it appears that you are loading the config file every time you resolve a control... maybe you could store the InputStreamResource in a static variable so that it isn't accessed on every load.

Brian

March 3. 2008 11:36 PM

Ian Suttle

@Brian - You are certainly right; thanks for the comment. I've updated the post with a more efficient example. Thank you!

Ian Suttle

March 4. 2008 06:24 AM

Brian

Looks much more efficient. I don't have any experience with Spring.Net yet; just Windsor, so I'll probably give it a shot to see the upfalls and downfalls.

Brian

May 26. 2008 12:14 PM

venu

Hi ian,
The sample which you have given for dependency injection is very useful.
I appreciate the way you explained the manner.thank you very much.

venu

May 26. 2008 12:17 PM

venu

Is there any sample which explains,how to use property list in the config file and call in the main application.

venu

May 27. 2008 10:05 AM

Ian Suttle

@Venu - Have you checked the Spring.NET site for these details. I'm still a rookie on the topicSmile.

Ian Suttle

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

August 21. 2008 12:16 PM

About Me

I'm Ian Suttle and I work for IGN Entertainment, a division of Fox Interactive Media.

Recent posts