View Full Version : What if an object I'm trying to configure IS a generic collection?
dolcept
03-28-2007, 10:11 PM
I've seen samples of configuring an object's property that happens to be a generic collection, but what if the object I'm trying to define is itself a generic collection?
There doesn't seem to be any support for this.
Instead of this (where the MyObject class has a List<int> property called Numbers)...
<object id="myObject" type="MyObject, Demo">
<property name="Numbers">
<list element-type="int">
<value>1</value>
<value>3</value>
<value>5</value>
<value>7</value>
<value>11</value>
</list>
</property>
</object>
...what if I wanted to define the actual Numbers list all by itself to be later referenced by other object definitions?
I'd love to be able to do something like this:
<collection id="myList" type="System.Collections.Generic.List<int>, mscorlib">
<value>1</value>
<value>3</value>
<value>5</value>
<value>7</value>
<value>11</value>
</collection>
I'm using Spring.NET 1.1 Preview 3. Is there support for doing this, or if not, are there plans for implementing this in the future? Any information would be much appreciated. Workarounds welcome.
Thanks,
--Paul
Aleks Seovic
03-30-2007, 07:42 PM
Hi Paul,
You might be able to do something like this:
<object id="myList" type="System.Collections.Generic.List<int>, mscorlib">
<property name="[0]" value="1"/>
<property name="[1]" value="3"/>
<property name="[2]" value="5"/>
<property name="[3]" value="7"/>
<property name="[4]" value="11"/>
</object>
Let me know if that works.
- Aleks
dolcept
03-30-2007, 09:09 PM
Thanks for the idea. I tried it and get an exception when creating the application context. Here's the inner exception and its stack trace:
Invalid agument: The value "1" is not of type "System.Int32" and cannot be used in this generic collection. Parameter name: value
at Spring.Objects.ObjectWrapper.SetPropertyValues(IPr opertyValues propertyValues, Boolean ignoreUnknown) in j:\release\Spring.Net\src\Spring\Spring.Core\Objec ts\ObjectWrapper.cs:line 349
at Spring.Objects.ObjectWrapper.SetPropertyValues(IPr opertyValues pvs) in j:\release\Spring.Net\src\Spring\Spring.Core\Objec ts\ObjectWrapper.cs:line 302
at Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory.ApplyPropertyValues(String name, RootObjectDefinition definition, IObjectWrapper wrapper, IPropertyValues properties) in j:\release\Spring.Net\src\Spring\Spring.Core\Objec ts\Factory\Support\AbstractAutowireCapableObjectFa ctory.cs:line 322
Taking it one step further, I tried with a collection of my own custom type--which I have aliases set up for already. (I'm using ref's here since these objects are successfully created previously in the config file)
<object id="myList2" type="System.Collections.Generic.List<TaskQueuePool>, mscorlib">
<property name="[0]" ref="testTaskQueuePool" />
<property name="[1]" ref="espTaskQueuePool" />
</object>
That gets a bit farther, but throws another exception. Again, the inner exception and stack trace:
Argument out of range: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
at Spring.Objects.ObjectWrapper.SetPropertyValues(IPr opertyValues propertyValues, Boolean ignoreUnknown) in j:\release\Spring.Net\src\Spring\Spring.Core\Objec ts\ObjectWrapper.cs:line 349
at Spring.Objects.ObjectWrapper.SetPropertyValues(IPr opertyValues pvs) in j:\release\Spring.Net\src\Spring\Spring.Core\Objec ts\ObjectWrapper.cs:line 302
at Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory.ApplyPropertyValues(String name, RootObjectDefinition definition, IObjectWrapper wrapper, IPropertyValues properties) in j:\release\Spring.Net\src\Spring\Spring.Core\Objec ts\Factory\Support\AbstractAutowireCapableObjectFa ctory.cs:line 322
Continuing on, if I take your original example and replace the "value" attribute with "expression"...
<object id="myList" type="System.Collections.Generic.List<int>, mscorlib">
<property name="[0]" expression="1"/>
<property name="[1]" expression="3"/>
<property name="[2]" expression="5"/>
<property name="[3]" expression="7"/>
<property name="[4]" expression="11"/>
</object>
...I get this same exact "argument out of range" exception.
I thought we may have had it figured out there for a second. :)
Thanks for the quick reply. Any other ideas?
--Paul
Aleks Seovic
03-30-2007, 09:17 PM
Let me play with it a bit and figure out if we can provide support for this.
- Aleks
hrothgarv
11-17-2008, 06:40 PM
hi,
i don't know if this has been answered in other posts, but here's a solution to an object that inherits a generic list and putting the configuration in the container.
First, you need to implement the constructors of the generic list your class inherits:
public class ScheduledTimers : List<IScheduledTimer>
{
public ScheduledTimers()
: base()
{ }
public ScheduledTimers(int capacity)
: base(capacity)
{ }
public ScheduledTimers(IEnumerable<IScheduledTimer> collection)
: base(collection)
{ }
}
and then simply cofigure the object like a regular object in the container:
<object id="timers" type="ScheduledTimer.ScheduledTimers, ScheduledTimer">
<constructor-arg index="0">
<list element-type="ScheduledTimer.IScheduledTimer, ScheduledTimer">
<ref object="timer1"/>
<ref object="timer2"/>
</list>
</constructor-arg>
</object>
thanks,
vBulletin® v3.7.3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.