Using specific generics in TypeMatching config

Topics: Policy Injection Application Block
Jul 29, 2011 at 8:44 AM

Does anyone know the syntax for using generics in typematching, when using configuration file?

<matchingRule name="ShouldBeRepository" type="TypeMatchingRule">
<param name="typeName" value="IRepository`1" />


The code above works great for using any IRepository that takes 1 argument. But let's say I only want to run some code for the TextRepository:


<matchingRule name="ShouldBeTextRepository" type="TypeMatchingRule">
<param name="typeName" value="IRepository`1[[SomeNameSpace.TextModel, SomeAssembly]]" />


The code above does not work...  Nor does any other scheme that I can come up with... Anyone got a clue which syntax to use?





Aug 1, 2011 at 10:29 AM


You need to specify the qualified assembly name of your generic type. To avoid confusion, you can use alias. Here is a sample code:

    <alias alias="IRepository" type="UnityConfigExample.IRepository`1, UnityConfigExample" /> 

Then in your matching rule:
<param name="typeName" value="IRepository[[SomeNameSpace.TextModel, SomeAssembly]]"

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us


Aug 1, 2011 at 11:03 AM


Thanks for the reply... unfortunately that doesn't work either. It doesn't crash... but the interceptor doesn't seem to think there is a match either...

This is what the classes/interfaces looks like:

interface IRepository<TEntity>

class WbRepository<TEntity> : IRepository<TEntity> where TEntity : WbObject

class WbText : WbObject

interface ITextRepository : IRepository<WbText>

class TextRepository : WbRepository<WbText>, ITextRepository


I instantiate an ITextRepository which is implemented by TextRepository according to unity mapping.

I then call SaveChanges which is part of the IRepository<TEntity> interface. According to my setup, the interceptor should then be triggered but it isn't.

As long as I place interceptor on IRepository` it works... but as soon as I try to add the type to the generic, it fails... even if I use the qualified assemblyname as recommended.

Actually, if I use the fully qualified name, it doesn't even work without generics.



<?xml version="1.0"?>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
      <typeAlias alias="string" type="System.String, mscorlib" />
      <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
      <typeAlias alias="ITextRepository" type="WI4Lab.Repository.Text.ITextRepository, WI4Lab" />
      <typeAlias alias="TextRepository" type="WI4Lab.Repository.Text.TextRepository, WI4Lab" />
      <typeAlias alias="ITextLogger" type="WI4Lab.Proxy.Text.ITextLogger, WI4Lab" />
      <typeAlias alias="SaveTextLogger" type="WI4Lab.Proxy.Text.SaveTextLogger, WI4Lab" />
      <typeAlias alias="ITextService" type="WI4Lab.Service.Text.ITextService, WI4Lab" />
      <typeAlias alias="TextService" type="WI4Lab.Service.Text.TextService, WI4Lab" />
      <typeAlias alias="IGenericRepository" type="WI4Lab.Repository.IRepository`1, WI4Lab" />
      <container name="container">
        <extension type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common" />
        <extension type="Interception" />
	<register type="ITextService" mapTo="TextService" >
          <lifetime type="singleton" />
	<register type="ITextLogger" mapTo="SaveTextLogger" >
          <lifetime type="singleton" />
        <register type="ITextRepository" mapTo="TextRepository" >
          <lifetime type="singleton" />
          <interceptor type="InterfaceInterceptor"/>
          <interceptionBehavior type="PolicyInjectionBehavior" />
          <policy name="SaveTextShouldLog">
            <matchingRule name="ShouldBeRepository" type="TypeMatchingRule">
                <param name="typeName" value="IGenericRepository[[WI4Lab.Model.WbText, WI4Lab]]" />
            <matchingRule name="ShouldHaveSaveChangesMethod" type="MemberNameMatchingRule">
                <param name="nameToMatch" >
                  <value value="SaveChanges"/>
            <callHandler name="ShouldBeLogged" type="SaveTextLogger">
              <lifetime type="singleton"/>


Aug 2, 2011 at 7:58 AM


It seems you needed the version and publickeytoken as well to get it to work... with fully qualified it works... Thank you for your help.

Aug 2, 2011 at 8:15 AM

I tried to reproduce the scenario, and yes, it didn't intercept the call. Sorry for the mistake on my last post. Anyway, like what you have observed, it only works when you specify the type name and not with the qualified assembly name. Unfortunately, the type name of IRepository<T> is still IRepository`1 regardless of any type specified on the type parameter <T>. You can request a feature that will support matching rule for generics with specific type parameter on the Unity Feature Request and Issue. Tracker.


Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Aug 2, 2011 at 8:57 AM

Thanks mirtul for sharing this out! :)


Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us