Friday, December 17, 2010

MS Castle Windsor [IOC Container] Cheat-Sheet

Martin Fowler
IOC Containers and the Dependency Injection pattern

Wiki: Castle Windsor

Fluent API
Fluent Registration API

INSTANCE REGISTRATION
- Can you register an existing instance of a type?
- Castle Windsor – Registering Existing Object Instances
- Can you register an existing instance of a type in the container?

REGISTRATION & RESOLUTION OF NAMED INSTANCE

IWindsorContainer.Register(Component.For<ClassType>().Named("instanceKey").Instance(instanceOfClassType));

ClassType retrievedInstance = IWindsorContainer.Resolve("instanceKey");

StackOverflow Thread

REGISTER & RESOLVE BASED ON INTERFACE


Register Types Implementing Specified Interface:

container.Register(
AllTypes.
FromThisAssembly(). // register all types from parent assembly
BasedOn). // that inherit/implement interface IDialogService
WithService.FromInterface(). // allow resolution based on interface
Configure(c => c.LifeStyle.Is(LifestyleType.Transient)) // construct new
);

Resolve Instance of Class by Interface

IDialogService dialogService = container.Resolve();



Note on Registering/Resolving Generic Lists by Instance
I have been unable to register & resolve generic lists, e.g. List, by instance.
However, if you wrap the generic list in a custom class, e.g.

public class IntList : List
{
}

then you can register & resolve by instance with no problems.

1 comment:

  1. well actually the more up to date documentation is at http://stw.castleproject.org/Windsor.MainPage.ashx

    ReplyDelete

comment: