开发者

Castle Windsor: Inject NameValueCollection vs. Dictionary

I've already done many configs where dictionaries are passed into services in the <parameters> block.

But what I find myself needing right now is to build a NameValueCollection (allowing multiple entries with the same key) or a Collection of KeyValuePair objects.

The reason for this is im not using this dictionary to look up b when given a, im basically using it to pass in a Tuple (pair) of (a,b) to be used later in code.

Im kind of new to castle windor and I was wondering how i would go about making a List of KeyValuePair's injected, or a NameValueCollection injected.

What i need is something that looks like this:

<component
    id="Shipper.Air"
    service="CheckoutSystem.Shipping.IShipper, Components"
    type="CheckoutSystem.Shipping.ConditionalShipper, Components">
    <parameters>
      <id>8AED4A27-B5C2-46ad-BEDE-58DCB38F60DA</id>
      <MustSatisfy>True</MustSatisfy>
      <Shippers>
        <dictionary>
          <item key="Ups">${Shipper.UpsAir}</item>
          <item key="CanadaPost">${Shipper.CanadaPostAir}</item>
        </dictionary>
      </Shippers>
      <Conditions>
        <NameValueCollection> <!-- START Example -->
          <item key="Ups">$(ShipperCondition.HeavyItems}</item>
          <item key="CanadaPost">${ShipperCondition.FarNorth}</item>
          <item key="Ups">${ShipperCondition.Default}</item>
        </NameValueCollection> <!-- END Example -->
      </Conditions>
 开发者_如何转开发   </parameters>
  </component>


You can handle this with a custom type converter. See the documentation for Type Converters.

A couple of sample type converters:

  • http://www.codewrecks.com/blog/index.php/2008/06/04/castle-windsor-no-converter-registered-to-handle-the-type/
  • http://mikehadlow.blogspot.com/2010/02/10-advanced-windsor-tricks-10.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜