views:

517

answers:

2

Is there a better way for using settings (generated by Visial Stidio Settings editor) in Spring.NET configuration file than using PropertyRetrievingFactoryObject:

  <object id="myUri" type="Spring.Objects.Factory.Config.PropertyRetrievingFactoryObject, Spring.Core">
    <property name="TargetObject">
      <object type="Properties.Settings, MyAssembly">
      </object>
    </property>
    <property name="TargetProperty" value="Default.MyUri" />
  </object>

  <object name="..." type="...">
    <property name="Uri">
      <ref object="myUri" />
    </property>
  </object>

?

It does not feel right to do this for every setting...

A: 

Thanks for reminding me why I really need to ditch Spring.Net and get myself a new DI framework!

Sorry, this is the only way I am aware of. Sucks, doesn't it?

Matt Howells
A: 

Hi,

First, why not just put the url in the Spring.NET configuration file ? Having multiple ways to configure your application can be a bit confused. If this file have been generated by Visual Studio because you added a Web Reference, you should change the 'Url Behavior' property of your Web references from 'Dynamic' to 'Static'. then you can remove all the stuffs VS generates, the settings files and the configuration code in the App.config/Web.config. To configure your proxy, just add it to the container and use DI to inject the Url property.

Anyway, you can achieve what you wanted to do with the Spring Expression language :

<object name="..." type="...">
  <property name="Uri" expression="T(Properties.Settings, MyAssembly).Default.MyUri">
</object>

Another solution is to use the VariablePlaceholderConfigurer component with the IVariableSource interface : http://www.springframework.net/doc-latest/reference/html/objects.html#objects-variablesource

<object type="Spring.Objects.Factory.Config.VariablePlaceholderConfigurer, Spring.Core">
   <property name="VariableSources">
      <list>
         <object type="MyCustomImplementationVariableSource, MyAssembly"/>
      </list>
   </property>
</object>

<object name="..." type="...">
  <property name="Uri" value="${MyUri}"/>
</object>

MyCustomImplementationVariableSource is an implementation of the IVariableSource that will resolve variables from where you want (for example from your Settings class).

  • Bruno
Spring Expression is what I need. Looks much cleaner.
Valery Tydykov