1

I am trying to inject an IList into a constructor and want to know if this is possible and if so could someone enlightern me? The fact its asking for a type converter makes me think I can't do it.

I tried....

<object name="DataLayer.DataServices.IGetAccountDatabase" type="DataServices.LinqToEntities.EntityDataModel, DataServices.LinqToEntities" singleton="false"></object>
<object name="DataLayer.DataServices.IGetAccountMessage" type="DataServices.VMX.VmxModel, DataServices.VMX" singleton="false"></object>

<object name="DataLayer.DataServices.IDataService" type="DataServices.NavigationModel, DataServices" singleton="false">
    <constructor-arg>
        <list element-type="DataLayer.DataServices.IGetAccount, DataLayer" >
          <idref object="DataLayer.DataServices.IGetAccountMessage"/>
          <idref object="DataLayer.DataServices.IGetAccountDatabase"/>
        </list>
    </constructor-arg>
</object>

Error creating object with name 'DataLayer.DataServices.IDataService' defined in 'file [D:\Workspace\DataServices\Main\Source\DataServices\TestResults\2010-07-30 11_58_31\Out\Spring.xml]' : Initialization of object failed : Unable to convert managed list element 'DataLayer.DataServices.IGetAccountMessage' from [System.String] into [DataLayer.DataServices.IGetAccount] during initialization of property 'constructor argument' for object 'DataLayer.DataServices.IDataService'. Do you have an appropriate type converter registered?

Any help appreciated. thnx

tjh7
  • 77
  • 1
  • 10
  • There is nothing wrong with this part of your xml configuration so this should work (and is definitely possible). Probably you've made a mistake somewhere else. Can you add the xml definitions for business.IGetAccount_A and business.IGetAccount_B? And do these objects both implement the interface business.IGetAccount? – Ronald Wildenberg Jul 30 '10 at 10:31
  • Are you sure the error message that you show is the exact error message that you receive from your application? – Ronald Wildenberg Jul 30 '10 at 10:32
  • I thinned everything down (removed some more constructor-args) and shortened names. Will add the rest of the config... – tjh7 Jul 30 '10 at 10:39
  • Updated the question with a simple example. Yes both objects implement the IGetAccount interface – tjh7 Jul 30 '10 at 11:30

1 Answers1

1

I think that if you specify the objects with an id instead of a name it should work (it is generally better to use id's). The idref element can only be used to refer to objects by id.

If the object DataLayer.DataServices.IDataService and the objects DataLayer.DataServices.IGetAccountMessage and DataLayer.DataServices.IGetAccountDatabase are part of the same xml file, you can also use the local attribute of idref. Then you get an error when the xml file is parsed, even before the dependencies will be resolved.

Ronald Wildenberg
  • 31,634
  • 14
  • 90
  • 133
  • Thanks very much I have got this working and you pointed me in the right direction. Basically it appears that the idref element passes the name of the obejct as a string whereas the ref element passes the object, hence the error I was getting. While looking at the idref element and the local attribute I came across this post... http://stackoverflow.com/questions/269845/spring-net-problem-with-idref-tag-in-config – tjh7 Aug 02 '10 at 08:58