views:

43

answers:

1

In castle windsor, when registering instances with a singleton lifecycle, is there a way to eagerly instantiate them (rather then having them initialized the first time they are injected)?

Update:

I figured some more details would be helpful here:

  1. These instances contain some initialization code that would be advantageous to run at startup time, that's why I'm interested in doing this.
  2. I'm registering quite a few of these instances using AllTypes.Pick(), so I'd prefer a solution that didn't involve me manually resolving each instance from the container seperately after I've built it up.
+2  A: 

Yes, you can using Startable Facility (which comes out of the box with Windsor):

container.AddFacility<StartableFacility>(
// optionally in v2.5
f=> f.DeferredStart()
);
container.Register(
   AllTypes.FromThisAssembly()
      .Pick().WhateverYouWant()
      .Configure(c => c.Start());

DeferredStart method is new in v2.5 and you can see here what it does and why it is advised to use it. The samples uses some of the new API in v2.5 but if you're using v2.1 it should give you an idea of how to achieve this.

In previous versions method Start() is called Startable()

Krzysztof Koźmic
Thank you! this is exactly what I was looking for.
DanP
One other question...my "start" logic is called in the constructor for the component; so how do I get this working using this method?
DanP
Use `Start` method, it will - just instantiate the object. If you have a method with startable logic on your component use `StartUsingMethod(c => c.YourStartMethod)`In other words - the example I showed does exactly what you want.
Krzysztof Koźmic
Alright..I see; which is equiv to c => c.Startable() in previous versions...no wonder they changed that, it's somewhat confusing :)
DanP
Yes - exactly :)
Krzysztof Koźmic