- 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!
- Posted by Ian Suttle on November 19, 2007
- Filed under .NET 3.5 | Visual Studio
I wanted to share my experience of going from Visual Studio 2008 Beta 2 to the RTM. Scott Guthrie recommends uninstalling Visual Studio 2008 Beta 2, .Net Framework Beta 2, and Visual Studio Web Authoring Component. When I uninstalled VS it seems to have taken the framework with it. Typically I wouldn't expect this coupling to occur however in the case of beta and no release license I don't completely excuse that possibility.
I've also uninstalled a few others just to wipe the slate as clean as possible (it's a beta, who knows). Other uninstalls include "MSDN Library for Visual Studio 2008 Beta 2 - ENU", "Microsoft SQL Server Compact 3.5" (and friends), and the "Microsoft .Net Compact Framework 3.5" or at least I think that's what it was called:).
In my opinion the uninstall processes are the most unpolished piece of the whole Beta 2 experience which was fantastic. A number of times the uninstaller would halt due to some program being used. Many times a simple "Retry" would resolve that one.
The install itself went smooth, but again took quite some time... now to find Team Explorer for 2008!
- Posted by Ian Suttle on November 19, 2007
- Filed under .NET 3.5 | Visual Studio
Microsoft has released Visual Studio 2008 to MSDN subscribers. Oddly however, it wasn't listed in my applications tree view. I got to the goods through this page: http://msdn2.microsoft.com/en-us/subscriptions/bb608344.aspx
This is also the first time I've seen Microsoft use Akamai to deliver a file. It's not your standard Microsoft BITS download add-in... maybe FireFox is giving it the bird.
- Posted by Ian Suttle on October 23, 2007
- Filed under Visual Studio
Download the sample code for this post.
I originally intended to write about customizing existing Visual Studio 2005 item templates such as the Class file to take the form of whatever you'd like, yet I decided that was the easy stuff. The real power is in creating your own item templates. You can likely discern how to customize an existing template based on this sample (email me if you have questions). There are a zillion reasons (literally!) why you may want to do this but a simple example that comes to mind is a custom exception. Granted the best practice of exceptions is to throw existing exception types but that would simply defeat the purpose of my example!
There are two important files included in this process:
1. Your custom file which is the template for the item
2. The .vstemplate file which provides some meta data about your custom item template
To spice up your custom template check out the available parameters to include in your file.
Here's a custom exception template which implements the four recommended constructors. I've called the file Exception.cs. Don't worry about naming conflicts with the base System.Exception type as the template specifies use of your project's root namespace. Additionally the user has the option to change the filename (seen here as $safeitemname$) or if the suggested name is kept a number is automatically appended to the filename such as Exception1.cs.
Exception.cs
Exception.vstemplate
Once you've created these files, zip them up into a file called, wait for it... Exception.zip. Drop that file in your "user item templates location" which you can find by going to Tools > Options > Projects and Solutions > General. So you have some general idea of where that might be on my system it's in C:\Documents and Settings\isuttle\My Documents\Visual Studio 2005\Templates\ItemTemplates\Visual C#.
Now for the feeling of accomplishment. Create or open a project and add a new item to it. You should see your "Exception Template" as an option under "My Templates."
If all goes well you'll have a file looking something like this:
Download the sample code for this post.
- Posted by Ian Suttle on July 26, 2007
- Filed under .NET 3.5 | Visual Studio
Scott Guthrie has officially announced the Beta 2 release of Visual Studio 2008 and the .Net Framework 3.5. You can see additional information on Scott Guthrie's blog. You can download the full version of Visual Studio 2008 here or get Visual Studio 2008 Express Editions here.
Among the various improvements offered, I'm pleased to see Unit Testing is now a part of the Professional version and not exclusive to Team versions only. Pro's a bit lighter on the pocket book
.
I've had great success with Beta 1 to date, so I'm quite excited to jump into Beta 2. If you're like me, you'll need to follow both the recommended installation notes in Scott's blog entry; one for all installations of Beta 2, and the second for users who have previous installations. If you have any additional information you'd like to share regarding VS 2008 Beta 2, please do!