Policy Injection Application Block Validation and Logging Examples - Aspect Oriented Programming in Enterprise Library 3.0
by David Hayden ( Florida ASP.NET Developer ), Filed: Enterprise Library 3.0
I had a blast presenting at the Orlando Code Camp this weekend. One of my presentations, Enterprise Library 3.0 – New and Improved!, discussed several topics, but focused on the new Validation Application Block and Policy Injection Application Block. The Policy Injection Application Block is a difficult topic to Grok in 20 minutes at a Code Camp, especially since it will not be complete and have configuration editor support until the final release of Enterprise Library v3.0 during the 1st week of April 2007.
You can read two of my tutorials on the Policy Injection Application Block here for additional information:
Both of the tutorials mentioned above use the TagAttributeMatchingRule to specify methods that will be intercepted by the Policy Injection Application Block. The TagAttributeMatchingRule looks like the following:
public interface IOrder
{
[Tag("Log")]
void Return(string reason);
}
However, the TagAttributeMatchingRule is not the only way to specify type methods to intercept. Based on the Februrary 2007 CTP you have the following Matching Rules and I have really only discussed one:

In an example I provided at the Orlando Code Camp and that is available for download, I used two other Matching Rules:
- TypeMatchingRule
- MemberNameMatchingRule
Used together these two rules can also specify the Return Method on the IOrder Interface without requiring any attributes be added:
public interface IOrder
{
void Return(string reason);
}
<policies>
<add name="Logging">
<matchingRules>
<add
type="...TypeMatchingRule..."
name="Type Matching Rule"
match="IOrder"
ignoreCase="false" />
<add
type="....MemberNameMatchingRule..."
name="Member Matching Rule"
match="Return"
ignoreCase="false" />
</matchingRules>
<handlers>
<add
name="Logging Handler"
type="...LogCallHandler..."
logBehavior="Before"
beforeMessage="Logging Return..."
includeParameterValues="true"
includeCallTime="true"
includeCallStack="false"
severity="Information">
<categories>
<add name="General" />
</categories>
</add>
</handlers>
</add>
</policies>
Now when you use the Policy Injection Application Block as follows:
IOrder order =
PolicyInjection.Create<Order, IOrder>();
order.Return("No change in size.");
The Policy Injection Application Block will log the reason for the order being returned before executing the Return Method. One could have also specified that the logging occur after the execution of the Return Method instead.
Timestamp: 3/25/2007 2:14:17 PM
Message: Logging Return
Category: General
Priority: 0
EventId: 1
Severity: Information
Extended Properties: reason - No change in size.
Conclusion
It will be easier to work with the Policy Injection Application Block once we can use the Enterprise Library Configuration Editor to configure it properly. This will happen when it is released during the first week of April 2007.
Source: David Hayden ( Florida ASP.NET Developer )
Filed: Enterprise Library 3.0