-
IOC and Castle Windsor
-
Yesterday I tried IOC container Windsor from Castle project.Itis very nice container and really powerful, not as Spring.netmaybe but more thin and easy to use. Now I will show a little exampe. I developed the Registry pattern in c# with IOC facilities of Windsor. In Windsor you expose your interfaces as services. My interface is:
using
System;
using
System.Collections.Generic;
using
System.Text;namespace Common.Registry
{
public interface IRegistry
{
string getCurrentLanguage();
string getCurrentUser();void setCurrentLanguage(string lang);void setCurrentUser(string username);
}
}
I developed 2 classes that create concrete services implementations.These classes in Windsor are marked as types.One of these classes is a registry web-session based.
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Web;namespace Common.Registry.Classes
{
public class SessionRegistry : IRegistry
{
#region
IRegistry Memberspublic string getCurrentLanguage()
{
return HttpContext.Current.Session[Constants.Language].ToString();
}
public string getCurrentUser()
{
return HttpContext.Current.Session[Constants.User].ToString();
}
public void setCurrentLanguage(string lang)
{
HttpContext.Current.Session[Constants.Language]=lang;
}
public void setCurrentUser(string username)
{
HttpContext.Current.Session[Constants.Language] = username;
}
#endregion
}
}
IOC is realized by configuration files. In my example I used a simple App.config but you can ues an external xml config file.
<?xml version="1.0" encoding="utf-8" ?><configuration>
<
configSections> <section
name="registry"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" /></configSections><registry>
<
components>
<
component id="staticregistry" service="Common.Registry.IRegistry,Common.Registry" type="Common.Registry.Classes.StaticRegistry,Common.Registry"></component><component id="sessionregistry" service="Common.Registry.IRegistry,Common.Registry" type="Common.Registry.Classes.SessionRegistry,Common.Registry"></component>
</
components></registry>
</
configuration>
Finally I wrote a simple test for see everything in action.
using
System;
using
System.Collections.Generic;
using
System.Text;
using
Castle.Core;
using
Castle.MicroKernel;
using
Castle.Core.Resource;
using
Castle.Windsor.Configuration.Interpreters;
using
Castle.Windsor;
using
NUnit.Core;
using
NUnit.Framework;
using
Common.Registry.Classes;
using
Common.Registry;namespace TestRegistry
{
[TestFixture]
public class TestRegistry
{
[
Test]public void Test1()
{
IWindsorContainer container =new WindsorContainer(
new XmlInterpreter(new ConfigResource("registry")));IRegistry tmp = container.GetService<Common.Registry.IRegistry>();
tmp.setCurrentLanguage(
"it");Assert.AreEqual("it", tmp.getCurrentLanguage());
container.Release(tmp);
}
}
}
Bye bye
Antonio
-
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