Is there way in Spring for .NET how to use translated message from an IMessageSource as constructor argument for other object in the xml application context file? Something like <spring:message> in Java. My example:
<!-- my message source -->
<object name="messageSource" type="MyMessageSource"></object>
<object type="MyLocalizedObject">
<!-- my object, where I need to pass tranlated message into the argument: -->
<constructor-arg name="localizedTitle" value=">{translated title.key}<"/>
</object>
Well, you could do it with an expression:
<object name="messageSource" type="MyMessageSource"></object>
<object type="q9257449_springmessage.MyClass, q9257449_springmessage">
<constructor-arg name="localizedTitle"
expression="#(messageSource).GetMessage('HelloMessage', 'mr.', 'Anderson')" />
</object>
There might be a better way to achieve this, but in the meantime this should get you going.
Related
I want to instantiate a System.Net.IPEndPoint with Spring.Net.
Here is my xml code:
<object id="MulticastAddress" type="System.Net.IPAddress" factory-method="Parse">
<constructor-arg value="239.239.239.1"/>
</object>
<object id="DestinationEndPoint" type="System.Net.IPEndPoint">
<constructor-arg name="address" ref="MulticastAddress"/>
<constructor-arg name="port" value="2010"/>
</object>
But this causes a Spring.Core.TypeMismatchException with additional information: Cannot convert property value of type [System.Net.IPAddress] to required type [System.Int64] for property ''.
IPEndPoint has two constructors:
public IPEndPoint(long address, int port);
public IPEndPoint(IPAddress address, int port);
It seems that spring uses the first constructor which is actually a bad idea.
So how can I tell spring that it should use the second constructor?
Update :
I figured out how to convert the IPAddress into a long:
<object id="DestinationEndPoint" type="System.Net.IPEndPoint">
<constructor-arg name="address" expression="T(System.Net.IPAddress).Parse('239.239.239.1')"/>
<constructor-arg name="port" value="2010"/>
</object>
But now I got the exception: Cannot convert property value of type [System.Int64] to required type [System.Net.IPAddress] for property ''.
Now it seems that Spring uses the other constructor. What is it!?
I found the solution by my self. Spring.Net is throwing this exceptions if a constructor does not fit to the passed arguments. But Spring.Net is catching this exception later and tries the next constructor. So my problem was that I has to tell VisualStudio that it shouldn't break by this exception -_-.
Could anyone please help me take a look at this issue since I can only create a Spring object type String? When I try to create another Spring object type, I get the below error
Error
Class Initialization method MessengerLyncSDK2013.Testcases.Test.UnitTest1.ClassInitialize threw exception. System.Configuration.ConfigurationErrorsException: System.Configuration.ConfigurationErrorsException: Error creating context 'spring.root': Error creating object with name 'serverPort' defined in 'config [D:\Working Projects\lync2013\MessengerLyncSDK2013\TestResults\thanh.viet.le_LGVN13307-WIN7 2014-03-17 11_17_21\Out\MessengerLyncSDK2013.DLL.config#spring/objects] line 9' : Could not resolve matching constructor. ---> Spring.Objects.Factory.ObjectCreationException: Error creating object with name 'serverPort' defined in 'config [D:\Working Projects\lync2013\MessengerLyncSDK2013\TestResults\thanh.viet.le_LGVN13307-WIN7 2014-03-17 11_17_21\Out\MessengerLyncSDK2013.DLL.config#spring/objects] line 9' : Could not resolve matching constructor..
Spring object in xml file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
<object id="connectServer" type="string">
<constructor-arg value="server.com"/>
</object>
<object id="serverPort" type="System.Int32" factory-method="Copy">
<constructor-arg index="0">
<value>5222</value>
</constructor-arg>
</object>
</objects>
</spring>
</configuration>
For more details, I am using visual studio 2010 with C#.
Try it, note for factory-method="Parse":
<object id="MyInt" type="System.Int32" factory-method="Parse">
<constructor-arg index="0">
<value>123</value>
</constructor-arg>
</object>
See also: How do I create a spring .Net standalone object of type Int32 defined in the IOC context file?
You can create an object with all your configuration and then just inject it:
<object id="ServerConfig" type"...">
<property name="ServerPort" value="5222"/>
...
</object>
<object id="Server" type"...">
<!-- Constructor injection -->
<constructor-arg name="configuration" ref="ServerConfig"/>
<!-- OR Property injection -->
<property name="Configuration" ref="ServerConfig"/>
</object>
In my dependency injection configuration file - I don't know (and can't find an example) on how to set into a member some predefined environment variable file.
Can't tell how to do it in Spring.Net - your help will be appreciated.
Use an EnvironmentVariableSource, the current docs are somewhat lacking at the moment: http://springframework.net/docs/1.3.2/reference/html/objects.html#objects-variablesource. I've written some additional docs, they'll probably be included in the next 2.0 release of the build. In the meantime you can also check this example code on Github https://github.com/serra/spring-net-examples/tree/master/Spring.IoCQuickStart.VariableSources.
Take a look to the VariablePlaceholderConfigurer object. It should do the job:
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="Spring.Objects.Factory.Config.EnvironmentVariableSource, Spring.Core"/>
</list>
</property>
</object>
<object type="MyObject">
<property name="MyProperty" value="${MyEnvironmentVariableName}"/>
</object>
I have an object that contains a property:
public Dictionary<string, Dictionary<string, List<ContextMenuItemModel>>> ContextMenuModel { get; set; }
How do I use Spring.Net to configure this property?
Well, configuring this in xml isn't pretty, consider switching to Spring.Net code config to configure your spring context in C#.
Anyway, to do this in xml, you use the constructors of the generic .net collections. For instance, List<T> takes an IList<T> constructor, so you can configure a list of strings as follows:
<object id="list1" type="System.Collections.Generic.List<string>">
<constructor-arg>
<list element-type="string">
<value>abc</value>
<value>def</value>
</list>
</constructor-arg>
</object>
Note that in xml you have to use <, because using < isn't legal xml. Setting generic collection values is discussed in the Spring.net docs.
A generic Dictionary<string, System.Collections.Generic.List<string>> can be configured in a similar manner, which is also discussed in this answer:
<object id="dic1" type="System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<string>>">
<constructor-arg>
<dictionary key-type="string" value-type="System.Collections.Generic.List<string>">
<entry key="keyToList1" value-ref="list1" />
<entry key="keyToList2" value-ref="list2" />
</dictionary>
</constructor-arg>
</object>
And you probably see the next one coming now:
<object id="dic0" type="System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<string>>>">
<constructor-arg>
<dictionary key-type="string" value-type="System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<string>>">
<entry key="keyToDic1 " value-ref="dic1" />
</dictionary>
</constructor-arg>
</object>
Which can be injected:
<object id="MyObject" type="MyNamespace.MyClass, MyAssembly">
<property name="ContextMenuModel" ref="dic0" />
</object>
This isn't really pretty, but you can slightly improve the readability of your xml using type aliases.
I'm using Spring.NET's IoC container and everything has been working just fine....until now. Somehow, in one of our previous releases, we introduced a circular dependency. Since we use setter based injection as opposed to constructor based injection, Spring.NET just kept humming along fine, but the behavior of our app changed.
Now I have a solution with a hundred or so components, and somewhere in that pile of components exists a circular dependency, which I now need to find.
Are there any tools that can take my Spring.NET config files and give me a graphical picture of my components and their dependencies?
AFAIK there isn't such a tool available, although there is one for spring for Java.
This thread on the spring.net forum
discusses the issue and proposes a solution.
I made a quick-and-dirty proof of concept based on Thomas Darimont's approach using QuickGraph.
For the following configuration file:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object id="a1" type="q7446068.ClassA, q7446068" >
<property name="MyOtherA" ref="a2" />
</object>
<object id="a2" type="q7446068.ClassA, q7446068" >
<property name="MyOtherA" ref="a1" />
</object>
<object id="a3" type="q7446068.ClassA, q7446068" />
</objects>
I was able to create the following dot file:
digraph G {
0 [label="a1"];
1 [label="a2"];
2 [label="a3"];
0 -> 1 [];
1 -> 0 [];
}
Which shows the circular dependency.
The code is available as a gist.