Andrew Duffy
Andrew Duffy

Reputation: 563

Windsor Castle Equivalent of Autofac's IStartable

I'd like to be able to implement this in my windsor castle container set up:

"For all types that implement IStartable in the current assembly register them and run the Start method for them."

Similar to what you can do using Autofac for things like registering Automapper mappings. eg

    public class MyBlahViewModelMapper : IStartable
{
    public void Start()
    {
        Mapper.CreateMap<MyBlahEntity, MyBlahViewModel>();
    }
}

Autofac does it automagically.... I'm thinking Windsor can't help me here?

Upvotes: 3

Views: 542

Answers (2)

Krzysztof Kozmic
Krzysztof Kozmic

Reputation: 27374

Windsor has its own IStartable interface. If you want Windsor to register your objects and create/run them immediately after that you'd use Startable Facility for that.

To clarify, there are two concepts here:

  1. IStartable interface, which provides Start and Stop methods. This is a lifecycle interfaces that provide lifecycle callbacks: Start being called right after a component instance gets created (after the constructor runs)

  2. Startable Facility, which forces your IStartable components to be instantiated and started immediately after installers have ran.

Here's what the code would look like:

container.AddFacility<StartableFacility>(f => f.DeferredStart());
container.Install(FromAssembly.This());
// by here all startable are started

If you're on Windsor 3.3 or later you can also manually trigger the startables to start (which is useful if you need to do some extra setup for them)

var flag = new StartFlag();

container.AddFacility<StartableFacility>(f => f.DeferredStart(flag));
container.Install(FromAssembly.This());
// do whatever else set up your app needs

// when ready, signal the flag
flag.Signal();
// by here all startable are started

Upvotes: 2

user2864740
user2864740

Reputation: 61875

The closest is Castle Windows Installers - they can trivially scanned from an assembly and installed (or 'started'). Installers are usually used to register components, but they can be used for other initialization as well.

Windsor uses installers (that is types implementing IWindsorInstaller interface) to encapsulate and partition your registration logic .. FromAssembly [makes] working with installers a breeze.

After creating an installer use one of the fluent configurations in the main IoC bootstrap, eg:

container.Install(
   FromAssembly.This());

Note that the order is unspecified; installers that must occur in an order must be specified with an explicit order to Install, possibly through a modified assembly reflector.

Upvotes: 0

Related Questions