Injection into Collections/Lists

May 27, 2008 at 7:41 PM
Edited May 27, 2008 at 7:42 PM
Is there a way to inject into list items? Let me illustrate what I mean. Let's say I have a service

public class MyTaskService: ITaskService
{
    private IList<TaskBase>  _tasks;

    public IList<TaskBase> Tasks
    {
       set
       {
          _tasks = value;
       }
    }
    public void Start()
    {
       foreach(TaskBase atask in _tasks)
          atask.Run();
    }
}

public abstract class TaskBase
{
    public abstract void Run();
}

public class TaskType1: TaskBase
{
....
}

public class TaskType2: TaskBase
{
....
}

public class TaskType3: TaskBase
{
....
}

I wanted a way to inject instances of the concrete TaskTypes (1..to..3) into the Task collection. In an XMl config maybe something like:

  <unity>
    <typeAliases>
      <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
      <typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity" />
    </typeAliases>

    <containers>
      <container name="containerOne">
        <types>

          <type name="task1"  type="MyProject.TaskBase,MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"  mapTo="MyProject.TaskType1,MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">           
            <lifetime  type="singleton" />
          </type>

          <type name="task2"  type="MyProject.TaskBase,MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"  mapTo="MyProject.TaskType2,MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">           
            <lifetime  type="singleton" />
          </type>

          <type name="task3"  type="MyProject.TaskBase,MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"  mapTo="MyProject.TaskType3,MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">           
            <lifetime  type="singleton" />
          </type>

          <type  type="MyProject.ITaskService,MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"  mapTo="MyProject.MyTaskService,MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">           
            <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
                <property name="Tasks">
                   <list propertyType="TaskBase" >
                      <item ref="task1" />
                      <item ref="task2" />
                      <item ref="task3" />
                   </list>
                </property>

              </typeConfig>
          </type>



        </types>
      </container>
    </containers>
  </unity>

Does unity support this kind of stuff? thanks.


May 27, 2008 at 10:02 PM
See the issue I wrote and the workaround I found ([workitem:2226]).

Far from slick but it works.
May 29, 2008 at 2:59 AM
I also need it very badly, can anybody pls guide us. I read on other thread it can be possible with TypeConverters. I tried, but no luck.