Monday, October 26, 2009

Castle Windsor Simple Tutorial

Inversion of Control using Castle Windsor

1. Download Castle Windsor here

2. Add the following Libraries to your Project reference


 Castle.Core.dll


 Castle.DynamicProxy2.dll


 Castle.MicroKernel.dll


 Castle.Windsor.dll





3. Add the following to your Web Config


  <configSections>


    <section  name="castle"


             type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>


  </configSections>


 


  <castle>


    <components>


      <component id="castleTest"


                 service="MyAssembly.ICastleTest, MyAssembly"


                 type="MyAssembly.CastleTest, MyAssembly"/>


    </components>


  </castle>




4. Code implementation


public class CastleTest : ICastleTest


{


    #region ICastleTest Members


 


    public string HelloWorld(string text)


    {


        return "my test is " + text;


    }


 


    #endregion


}


 


public interface ICastleTest


{


    string HelloWorld(string text);


}




5. In your controller


    public ActionResult About()


    {


        WindsorContainer container = new WindsorContainer(new XmlInterpreter());


        ICastleTest service = container.Resolve<ICastleTest>("castleTest");


 


        ViewData["castleTest"] = service.HelloWorld("hello world");


 


        return View();


    }


No comments:

Post a Comment