Injecting a Generic Collection

Jan 16, 2009 at 2:05 AM
I am new to Unity and I need to inject a property in a List<T>.

I have the following classes:

public class Person: IPerson {

private List<IAddress> _addressList;

public List<IAddress> addressList;{
get { return _addressList; }
set { _addressList = value; }
}

}

public class Address: IAddress {
...
}

How can I define de configuration file to Inject the Addess class in the List<IAddress>.

Thanks in advance.

<!-- / message -->
Jan 20, 2009 at 2:22 AM
Edited Jan 20, 2009 at 6:53 AM
Sample config:

 

type type="Custom.IAddress, Custom" mapTo="Custom.Address, Custom" />

 

<

type type="Custom.IPerson, Custom" mapTo="Custom.Person, Custom">

 

<

typeConfig>

 

<

property name="AddressList" propertyType="System.Collections.Generic.List`1[[Custom.IAddress, Custom]]" />

 

</

typeConfig>

 

</

type>

 

<

type type="System.Collections.Generic.List`1[[Custom.IAddress, Custom]]">

 

<

typeConfig>

 

<

constructor>

 

</

constructor>

 

</

typeConfig>

 

</

type>

 

</

types>

 

Note that I also registered the List<IAddress> type in the type mappings and specified a default constructor.  The reason for this is that the List type has 2 constructors, each with a single parameter.  As we know, Unity will take the greediest constructor so you need to instruct it to use the parameterless constructor.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Jan 21, 2009 at 12:37 PM

Hi Sarah,

Very soon I will test it

Thank you very much

Feb 3, 2009 at 8:42 AM
Hi Sarah,

I have a similar problem. I am using Unity with interception extention to log all the method calls.

here is my extention config for call handlers

                <callHandlers>
                  <callHandler type="LogCallHandler" name="LogCallHandler">
                    <injection>
                      <constructor>
                    </constructor>
                      <property name="IncludeParameters" propertyType ="boolean">
                        <value value="true" type="boolean"/>
                      </property>
                    </injection>
                  </callHandler>
                </callHandlers>

It works well except I can't specify the log category. it is now logging in the default category. I am sticking into this for days. can you help me to config the generic list<string> property "Categories" , How to config the elements for the list?

Your help is highly appreciated.


Feb 4, 2009 at 4:43 AM
I was actually trying to find out the same thing while I was trying to answer mvbaffa's question.  Haven't found the answer yet, it's nowhere in the documentation.  There is a sample for arrays though.  Anyway, I'll keep on digging and I'll let you know.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com