Multiple registrations with Castle Windsor

347 Views Asked by At

I have the following situation in a WPF application:

public class ExpenseView : UserControl, IAccountingView {}

// just a marker, the contract is in IViewWindow
public interface IAccountingView : IViewWindow {}

And I need to be able to register ExpenseView to be resolvable in two ways, as concrete type, ExpenseView and as IAccountingView (maybe as another interface as well).

I'm registering the types like this:

// I need a collection of IViewWindow to be used
Container.Register( Classes.FromAssemblyInDirectory( myTypes )
    .BasedOn<IViewWindow>()
    .WithServiceFromInterface()
    .LifestyleTransient()
    );

// all other types don't have interfaces, are used as concrete types.
Container.Register( Classes.FromAssemblyInDirectory( myTypes )
    .Where( type => type.IsPublic )
    .WithServiceSelf()
    .LifestyleTransient()
    );

Now, this works, ExpenseView and all other types are instantiated, except that when I need to use

var newInstance = container.Resolve( iView.ViewType );

to get another instance, where iView.ViewType is the concrete type ExpenseView (as per the example), I get this exception:

'Castle.MicroKernel.ComponentNotFoundException'  
No component for supporting the service ExpenseView was found.

Any ideas why is this happening and how to make it work?

1

There are 1 best solutions below

1
On BEST ANSWER

There is a good explanation of various registration options here: http://docs.castleproject.org/Windsor.Registering-components-by-conventions.ashx

To register all IViewWindow implementors also with their concrete type you can do:

Container.Register( Classes.FromAssemblyInDirectory( myTypes )
    .BasedOn<IViewWindow>()
    .WithServiceFromInterface()
    .WithServiceSelf()
    .LifestyleTransient()
    );