tags:

views:

23

answers:

2

I need to inject a collection of type IList<IActivity> into a constructor. I am using Unity 2.0 and configuring it using XML.

I was able to do this by just injecting an array in Unity 1.2. I have just upgraded to Unity 2.0 and my configuration has now stopped working in this specific area. (I have resolved all the trivial changes to the schema.)

My constructor looks like this:

public DoSomethingWorkflow(IList<IActivity> activityCollection)
{
  ...
}

My config looks like this:

<alias alias="IListOfIActivity" type="System.Collections.Generic.IList`1[[MyProject.IActivity, MyProject]], mscorlib"/>

<alias alias="IActivityArray" type="MyProject.IActivity[], MyProject" />

<register type="IWorkflow" mapTo="Workflow" name="DoSomethingWorkflow">
    <constructor>
      <param name="activityCollection" type="IListOfIActivity">
        <array>
          <dependency name="DoSomethingActivity1"/>
          <dependency name="DoSomethingActivity2"/>
        </array>
      </param>
    </constructor>     
</register>

If I run the config as above I get an error message saying "The configuration is set to inject an array, but the type IList`1 is not an array type".

If I change it to the way it used to work (injecting the type alias IActivityArray) I get an error message saying "Configuration is incorrect, the type MyProject.Workflow does not have a constructor that takes parameters named activityCollection".

If I remove the type attribute from the constructor element completely, I get the same error message as above.

The only way I can get this to work is to change the constructor to accept an IActivity[] array instead.

So is this a bug in Unity, as an array is an IList and should find the constructor? Or am I missing something in my config?

A: 

What I would do is create a type ActivityList that derives from IList<IActivity>.

class ActivityList : IList<IActivity>
{ /* */ }

This kind of stuff inside a config file:

System.Collections.Generic.IList`1[[MyProject.IActivity, MyProject]], mscorlib

is hard to understand and maintain.

bakopanos costas