Enterprise Library 3 Policy Injection Application Block Example
From 1 year I am interesting about AOP.I tried Spring.NET and now I am using new features of MS Enterprise Library 3.x.
Here I am posting some code that uses new Policy Injection Application Block built in Enterprise Library 3.x. I begin from business objects arriving to Factories calls. It is an example similar to the one in slides from Microsoft and it is about caching.
public class HTMLContentDataProvider : DataProviderTransactableBase, IHTMLContentDataProvider
{
#region IHTMLContentDataProvider Members
[
CachingCallHandler(2, 0, 0)]public CMS.BusinessObjects.HTMLContentLocalesDictionary GetAllContentsByTopic(int topicId)
{
HTMLContentLocalesDictionary objs = new HTMLContentLocalesDictionary();using (DbCommand c = this.CurrentDatabase.GetSqlStringCommand("SELECT * FROM HTMLContents WHERE HTMC_TopicId=@id"))
{
CurrentDatabase.AddInParameter(c,
"id", DbType.Int32, topicId);
IDataReader dr = this.CurrentDatabase.ExecuteReader(c);using (dr) {
while (dr.Read()) {
HTMLContent tmp = BuildHTMLContenteFromDataRow(dr);if(!objs.ContainsKey(tmp.Language.Trim()))
objs.Add(tmp.Language.Trim(),tmp);
}
}
return objs;
}
}
...................
This is the object where I want to inject caching policy.I used an attribute where I say that I want to cache content for 2 hours. For every method I can apply some policies by attribute or using configuration and matching rules. Base class DataProviderTransactableBase inherits from MarshalByRefObject. This permits to create a proxy object that is a placeholder for apply our policies.
public
class DataProviderTransactableBase : MarshalByRefObject
{
.........
But it is not sufficient, we must create our object version using PolicyInjection factory and calling method by proxy object.
public class FastHTMLContentDataProvider : MarshalByRefObject, IFastHTMLContentDataProvider
{
#region IFastHTMLContentDataProvider Memberspublic HTMLContentLocalesDictionary GetAllContentsByTopic(int topicId)
{
HTMLContentDataProvider obj = PolicyInjection.Create<HTMLContentDataProvider>();return obj.GetAllContentsByTopic(topicId);
}
...................
I hope that it will be useful for someone...
Tonio