Home

Castle Stronghold

Passing arrays, lists and dictionaries

The MicroKernel includes TypeConverters for arrays lists and dictionaries (Hashtable). This document depicts their usage.

Arrays

You can use arrays to all element types supported by the Conversion SubSystem. Given the following component:


public class MyComponent
{
    private String[] hosts;

    ...

    public String[] Hosts
    {
        get { return hosts; }
        set { hosts = value; }
    }
}

You can set the hosts array using the following configuration:


<component id="mycomponent">
  <parameters>
    <Hosts>
      <array>
        <item>server1</item>
        <item>server2</item>
        <item>server3</item>
      </array>
    </Hosts>
  </parameters>
</component>

The array will automatically be created as an String array.

Arrays of Services

You can also pass an array where each element is a component. For example:


public class MyComponent
{
    private IEmailSender[] senders;

    public MyComponent(IEmailSender[] senders)
    {
        this.senders = senders;
    }

}

You can set the senders array using the following configuration:


<component id="mycomponent">
  <parameters>
    <senders>
      <array>
        <item>${smtp.sender}</item>
        <item>${sendmail.sender}</item>
        <item>${imap.sender}</item>
      </array>
    </senders>
  </parameters>
</component>

Lists

An IList or ArrayList can also be used by your component to allow an ArrayList to be passed along. Consider the following component:


public class MyComponent
{
    private IList hosts;

    public MyComponent(IList mainHosts)
    {
        hosts = mainHosts;
    }

    ...
}

You can set the hosts array using the following configuration:


<component id="mycomponent">
  <parameters>
    <mainHosts>
      <list>
        <item>server1</item>
        <item>server2</item>
        <item>server3</item>
      </list>
    </mainHosts>
  </parameters>
</component>

The default Type for each list item is String. You can specify a different Type using the type attribute:


<component id="mycomponent">
  <parameters>
    <ports>
      <list type="System.Int32, mscorlib">
        <item>80</item>
        <item>81</item>
        <item>8080</item>
      </list>
    </ports>
  </parameters>
</component>

Lists of Services

Passing a list where each element is a service is also supported, as long as you specify the service. For example:


public class MyComponent
{
    private IEmailSender[] senders;

    public MyComponent(IEmailSender[] senders)
    {
        this.senders = senders;
    }

}

You can set the senders list using the following configuration:


<component id="mycomponent">
  <parameters>
    <senders>
      <array type="Namespace.IEmailSender, AssemblyName">
        <item>${smtp.sender}</item>
        <item>${sendmail.sender}</item>
        <item>${imap.sender}</item>
      </array>
    </senders>
  </parameters>
</component>

Remember: as the type defaults to string, if you forget to specify the type attribute the list will be populated with strings.

Dictionaries

Given the following component:


public class MyComponent
{
    private IDictionary props;

    public MyComponent(IDictionary props)
    {
        this.props = props;
    }

    ...
}

You can set the hosts dictionary using the following configuration:


<component id="mycomponent">
  <parameters>
    <props>
      <dictionary>
        <entry key="connectionstring">connection string here</entry>
      </dictionary>
    </props>
  </parameters>
</component>

You can also specify the type for the key and for the value:


<component id="mycomponent">
  <parameters>
    <props>
      <dictionary keyType="System.String, mscorlib" valueType="System.String, mscorlib">
        <entry key="connectionstring">connection string here</entry>
      </dictionary>
    </props>
  </parameters>
</component>

The type for the key and for the value can be overridden for each entry:


<component id="mycomponent">
  <parameters>
    <props>
      <dictionary keyType="System.String, mscorlib" valueType="System.String, mscorlib">
        <entry key="connectionstring">connection string here</entry>
        <entry keyType="System.Int32, mscorlib" valueType="System.Int32, mscorlib" key="1">10</entry>
      </dictionary>
    </props>
  </parameters>
</component>
Google
Search WWW Search castleproject.org