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
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
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.
well actually the more up to date documentation is at http://stw.castleproject.org/Windsor.MainPage.ashx
ReplyDelete