3

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?

Andrew Duffy
  • 795
  • 2
  • 17
  • 37
  • I guess I'm looking for a way to split out all my Automapper definitions..... my enviro is using Windsor Castle as the IOC Container. – Andrew Duffy Aug 25 '15 at 04:21

2 Answers2

2

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
Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115
0

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.

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • Thanks for the input.... I'm using installers... not sure how they will help me call the start method on 'n' classes which implement a particular interface – Andrew Duffy Aug 25 '15 at 05:06
  • @AndrewDuffy Implement n-classes with the IWindsorInstaller interface and implement the Install method. Forget it is called 'Install' and not 'Start'. If not possible/wanting to use the IWindsorInstaller interface then there is no 'built-in' way to do such, but such could be done - and still within Castle Windsor - by registering multiple IMyStartable types (eg.) and using `ResolveAll` and invoke the 'Start' method on each in turn. Castle Windsor can of course register all components for the given interface through it's reflection-fluent API. – user2864740 Aug 25 '15 at 05:27
  • It would also probably be possible to re-purpose parts of the fluent API (or inspect the source code of) for this purpose .. but the two methods discussed above work 'inside' the Castle Window built-in facilities. – user2864740 Aug 25 '15 at 05:29