- Posted by Ian Suttle on March 3, 2008
- Filed under .Net Framework | Visual Studio
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!