Hi, nice to have you read this again. This is part 4 of the series I’m doing on the existing .Net IoC Containers that I found interesting. After a brief introduction of what is an IoC in my option, I presented Autofac,,StructureMap and Ninject2.
In this post we’ll be exploring the Spring.NET IoC container. This is a xml configurable IoC by design. I’m using the latest version of it Spring.NET 1.3 RC1, hopefully it’s stable enough and that it brings something new to the table, by new I mean it uses the .NET 3.5 capabilities.
You can get the latest version of Spring.NET from http://www.springframework.net/. You will probably have to complete some kind of a registration form. I know I did.
Before you read any further I have to warn you that Spring.NET is a beast in the sense that is hard to configure and I don’t recommend using it.
Just as you would have done in Java once you get the configuration part out of the way it gets easier to use this framework. There is however a gotcha, after you get a valid IApplicationContext you are forced to use “magic strings” in your program to get to certain registered instances. You could probably define your own wrapper over the default registry methods, but that’s something you should explore on your own.
Let’s see how everything worked out. First I got the instance of the IApplicationContext using the line below:
IApplicationContext appContext = ContextRegistry.GetContext("spring.root");
I also configured the App.config file like so:
<?xml version="1.0" encoding="utf-8" ?>
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
<resource uri="config://spring/objects" />
<description>Definitions of objects to be exported.</description>
type="IoCContainers.Components.Services.ConsoleWriteString, IoCContainersSpringNET" />
type="IoCContainers.Components.Services.ConsoleClearScreen, IoCContainersSpringNET" />
type="IoCContainers.Components.Functions.AlarmFunctionState, IoCContainersSpringNET" >
type="IoCContainers.Components.Functions.NewLineFunctionState, IoCContainersSpringNET" >
type="IoCContainers.Components.Functions.CalculatorFunctionState, IoCContainersSpringNET" >
type="IoCContainers.Components.Services.ConsoleInputServiceImpl, IoCContainersSpringNET" >
Word of caution: The context part of the configuration is mandatory, it will through an error otherwise. The objects section however is referenced in the spring/context using the resource uri=”..” tag
Very disappointing to me was the fact that I had to use the IApplicationContext instance I got from the ContextRegistry like this to get my object instance:
var shortcutService = appContext.GetObject( "ConsoleInput" ) as IConsoleInputService;
One other thing I believe that by default the objects are configured using a Singleton behaviour. I didn’t use this library much outside the Java environment, and I have to say I like the .NET IoC’s that we’ve explored way better than this approach.
On the other hand there is a lot more to Spring than this, so as an enterprise middleware IoC Container it probably shines but that is for you to decide.
As always as posted the source code on codeplex and you can download it here.