How to configure the NamespaceMatchingRule(IEnumerable<MatchingInfo>) constructor in the unity xml configuration file?

Jan 16, 2012 at 9:36 AM

How to configure the NamespaceMatchingRule(IEnumerable<MatchingInfo>) constructor in the unity 2.0 xml configuration file?

Jan 17, 2012 at 12:47 AM
Edited Jan 17, 2012 at 4:29 AM

Here is how I was able to get it to work:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
    <container>
      <extension type="Interception"/>
      
      <interception>
        
        <policy name="Policy">
          <matchingRule name="Namespace Matching Rule" type="NamespaceMatchingRule">
            <constructor>
              <param name ="matches">
                <array type="Microsoft.Practices.Unity.InterceptionExtension.MatchingInfo, Microsoft.Practices.Unity.Interception">
                  <dependency name="MatchingInfo_NS1"/>
                  <dependency name="MatchingInfo_NS2"/>
                </array>
              </param>
            </constructor>
          </matchingRule>
      
          <callHandler name="MyCallHander" type="UnityPolicyInterception.MyCallHandler, UnityPolicyInterception" />
        
        </policy>
      </interception>

      <register type="Microsoft.Practices.Unity.InterceptionExtension.MatchingInfo, Microsoft.Practices.Unity.Interception" name="MatchingInfo_NS1">
        <constructor>
          <param name="nameToMatch" value="UnityPolicyInterception.NS1"/>
        </constructor>
      </register>
      <register type="Microsoft.Practices.Unity.InterceptionExtension.MatchingInfo, Microsoft.Practices.Unity.Interception" name="MatchingInfo_NS2">
        <constructor>
          <param name="nameToMatch" value="UnityPolicyInterception.NS2"/>
        </constructor>
      </register>
      
      <register type="UnityPolicyInterception.IAnimal, UnityPolicyInterception" mapTo="UnityPolicyInterception.NS1.Dog, UnityPolicyInterception" name="Dog">
        <interceptor type="InterfaceInterceptor"/>
        <policyInjection/>
      </register>
      <register type="UnityPolicyInterception.IAnimal, UnityPolicyInterception" mapTo="UnityPolicyInterception.NS2.Cat, UnityPolicyInterception" name="Cat">
        <interceptor type="InterfaceInterceptor"/>
        <policyInjection/>
      </register>
    </container>
  </unity>
</configuration>

Basically, I created named instance of MatchingInfo -- one for each namespace.  Then for the IEnumerable<MatchingInfo> I configure a MatchingInfo array that resolves the dependencies by name.

 

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Jan 17, 2012 at 5:41 AM
Edited Jan 17, 2012 at 5:46 AM

Thanks @randylevy it worked like a charm. I spent nearly half of my day to search on how to implement this using xml configuration file.