Configuration of Policy Injection

Apr 14, 2009 at 3:52 PM

Hi,

 

 

We are using Unity 1.2 on a project to do our IoC.  Now we would like to integrate some policies (AOP) on objects resolved by Unity.

 

I would prefer to use Unity “purely” as oppose to rigging some integration with Policy Injection Application Block (PIAB).  PIAB seems to be on the path of obsoleteness, being superseeded by Unity.  I'm used to Unity & I'm also used to Policy Injection, I now want to use both functionalites.

So…  I’m looking at Ent Lib doc and I can’t find a full "A to Z" sample of how to do this using solely configuration (most examples I found were using the runtime API).  The closest I found was a generic discussion:

 

http://msdn.microsoft.com/en-us/library/dd203168.aspx

 

Where no real policy is applied in configuration.  Also, I found this:

 

http://msdn.microsoft.com/en-us/library/dd203179.aspx

 

Which talk about the Tag matching rule I want to use, but doesn’t explain how to configure it.

 

Is it me or the documentation is falling a little short on that topic?  I also looked at the Quick Starts but none is using configuration.

 

Could you tell me where I could find a simple sample having injection of policies using some matching rules?

 

 

Cheers,

Apr 16, 2009 at 7:08 AM

Hi Vincent, there isn't a full "A to Z" example in the docs but the it did elaborate the configuration schema for unity which includes the <policies> element.  Have you read the Source Schema for the Unity Application Block section?  The discussion on the <policies> element should get you started.  If you still have trouble after going through it, you can always ask here.

Here's my sample config.  I have an interface IFoo having a method named Greeting(string name) implemented by class Foo.  I also have a custom call handler (MyCustomHandler) which just appends some string to the parameter passed to the Greeting method.  What my config does is add a TransparentProxyInterceptor (<interceptor> element), add a policy which would allow interception of the Greeting method by defining a MethodNameMemberMatchingRule applying MyCustomHandler.
 

<container>

 

<

extensions>

 

<

add type="Microsoft.Practices.Unity.InterceptionExtension.Interception, Microsoft.Practices.Unity.Interception" />

 

</

extensions>

 

<

extensionConfig>

 

<

add name="interception" type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationElement, Microsoft.Practices.Unity.Interception.Configuration">

 

<

interceptors>

 

<

interceptor type="Microsoft.Practices.Unity.InterceptionExtension.TransparentProxyInterceptor, Microsoft.Practices.Unity.Interception">

 

<

key type="CustomObjects.IFoo, CustomObjects"></key>

 

</

interceptor>

 

</

interceptors>

 

<

policies>

 

<

policy name="GreetingPolicy">

 

<

matchingRules>

 

<

matchingRule name="GreetingRule" type="Microsoft.Practices.Unity.InterceptionExtension.MemberNameMatchingRule, Microsoft.Practices.Unity.Interception">

 

<

injection>

 

<

constructor>

 

<

param name="nameToMatch" parameterType="System.String">

 

<

value value="Greeting" type="System.String"/>

 

</

param>

 

<

param name="ignoreCase" parameterType="System.Boolean">

 

<

value value="true" type="System.Boolean" />

 

</

param>

 

</

constructor>

 

</

injection>

 

</

matchingRule>

 

</

matchingRules>

 

<

callHandlers>

 

<

callHandler name="myHandler" type="CustomObjects.MyCustomHandler, CustomObjects">

 

<

lifetime type="singleton" />

 

<

injection>

 

<

constructor>

 

<

param name="order" parameterType="System.Int32">

 

<

value value="1" type="System.Int32"/>

 

</

param>

 

</

constructor>

 

</

injection>

 

</

callHandler>

 

</

callHandlers>

 

</

policy>

 

</

policies>

 

</

add>

 

</

extensionConfig>

 

<

types>

 

<

type type="CustomObjects.IFoo, CustomObjects" mapTo="CustomObjects.Foo, CustomObjects" />

 

</

types>

 

</

container>


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

Apr 16, 2009 at 11:36 PM
Thank you Sarah!

I've also found an example on http://www.nablasoft.com/alkampfer/index.php/2009/01/31/unity-and-aop-in-enterprise-library/.  It was quite a coincidence as the code described wasn't using configuration but in the code sample there was the full configuration only commented out!

I think it would be worth it for me to write a quick article on a public web site so that this gets publically documented and easily searchable.


Cheers,

Vincent-Philippe Lauzon
Apr 17, 2009 at 12:17 AM
Have you looked at the recently release Hands on lab for interception? That goes into a lot more detail of how to configure interception with the various options, both through Unity and using PIAB.
Apr 18, 2009 at 12:03 AM
Are you talking about the HOL available at http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=93a5e18f-3211-44ef-b785-c59bcec4cd6f?

I just downloaded it and didn't find any injection samples in the configuration file.  I did a search for "<policy" on all the *.config file and didn't find anything.  It's odd since the download page does mention "configuring injection through API and files".

Do I miss something?