IoC in .NET part4: Spring.NET

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");

(http://11011.net/software/vspaste)I also configured the App.config file like so:

Definitions of objects to be exported.

<object id="Alarm" type="IoCContainers.Components.Functions.AlarmFunctionState, IoCContainersSpringNET" > <constructor-arg name="clearScreen"> <ref object="ClearScreen"/> <constructor-arg name="writerString"> <ref object="WriteString"/>

<object id="NewLine" type="IoCContainers.Components.Functions.NewLineFunctionState, IoCContainersSpringNET" > <constructor-arg name="clearScreen"> <ref object="ClearScreen"/> <constructor-arg name="writeString"> <ref object="WriteString"/> <object id="Calculator" type="IoCContainers.Components.Functions.CalculatorFunctionState, IoCContainersSpringNET" > <constructor-arg name="clearScreen"> <ref object="ClearScreen"/> <constructor-arg name="writeString"> <ref object="WriteString"/>

<object id="ConsoleInput" type="IoCContainers.Components.Services.ConsoleInputServiceImpl, IoCContainersSpringNET" > <constructor-arg name="functionStates"> <ref object="Alarm"/> <ref object="NewLine"/> <ref object="Calculator"/>

(http://11011.net/software/vspaste)

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;

(http://11011.net/software/vspaste)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.

This article is part of the GWB Archives. Original Author: Sharpoverride

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.