Saturday, February 4, 2012

Configurations of application using IoC, Spring.NET

I have being using Spring when I worked in Java world. Now that I'm in C# part of the world, I'd naturally go with Spring.NET. To be honest, before using Spring.NET, I did not even heard about StructureMap, Windsor, Autofac, NInject, let alone the Funq, Munq, Light.Core, etc. In general, I could live with XML configuration, and some configuration feature derived from XML really makes passing configuration data much easier, by putting below object definition in the container, you can isolate your often changed settings to a file without writing any glue code:

  <object id="SYSTEM.ConfigPropertyHolder" type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
<property name="LastLocationOverrides" value="true"/>
<property name="ConfigSections" value="config.app"/>
<property name="Locations">
<list>
<value>~/Config/config.app.xml</value>
</list>
</property>
</object>



now I can use a file named "config.app.xml", in folder Config, to hold my config settings:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="config.app" type="System.Configuration.NameValueSectionHandler, System" />
</configSections>

<config.app>
<add key="value1" value="some data" />
<add key="value2" value="other data" />
...
</config.app>
</configuration>



and in the container definition xml file, I can use the settings defined in the config file:

  <object id="obj_id" type="type.name">
<constructor-arg name="para1name" value="${value1}"/>
<property name="pro1name" value="${value2}"/>
</object>



And since most of Spring.NET's objects are defined with an id, there is even a further way to do the customization, using another class "Spring.Objects.Factory.Config.PropertyOverrideConfigurer, Spring.Core". Put such an object definition in container,


<object id="SYSTEM.ConfigPropertyOverride" type="Spring.Objects.Factory.Config.PropertyOverrideConfigurer, Spring.Core">
<property name="IgnoreResourceNotFound" value="true"/>
<property name="ConfigSections" value="config.system"/>
<property name="Locations">
<list>
<value>~/Config/config.sys.xml</value>
</list>
</property>
</object>



you gain the capability to directly change object properties in config.sys.app:

  <config.system>

<!--the key must be a <object_in_context>.<property>-->
<add key="obj_id.someProperty" value="12345"/>
<add key="another_obj.anotherProp" value="good!"/>
...
</config.system>



Ugly or not, it's really convenient and powerful. In general, Spring.NET is really a feature rich software package, not only a IoC container. And also the document is really good, comparing to most other OSS projects.

So what's the problem? The problem is I'm getting more and more tired with the angle brackets, and I would like to try something new, like NInject, Autofac.

2 comments:

  1. what's wrong with just using CodeConfig? Autofac is nice because of it's callback support through anonymous delegates, and configurable scoping. But Spring has so much more to offer than just a DI. I'm trying to find a healthy medium.

    ReplyDelete
    Replies
    1. Hi Jon, yes, I'll also try codeConfig. The main reason is I have lived long enough in Spring world, I'd like to taste the rest of the world. Due to radical C# language change, more chances are opened in a lot of areas than Java world (ORM, etc.), I'd like to see more:)

      Delete