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

Published 19 December 2007 11:29 AM by antrad
Filed under:

Comments

No Comments

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP
  • Social Bookmarking
    Online Shopping
    asp.net hosting
    UK online local dating