Castle Windsor - register all interfaces using the factory method

I have several interfaces:

  • IFirstProvider
  • ISecondProvider
  • IThirdProvider
  • etc..

I am trying to register all of these interfaces so that they use the factory method to get the instance:

container.Register
    (  
        AllTypes
            .FromThisAssembly()
            .Where(t => t.IsInterface && t.Name.EndsWith("Provider"))
            .Configure(c => c.UsingFactoryMethod(f => f.Resolve<DictionaryAdapterFactory>().GetAdapter<object>(c.ServiceType, session))
    );

But this does not seem to work. Instead, I should use a loop forto register all of these interfaces:

List<Type> providers = new List<Type>
    (
        Assembly
            .GetExecutingAssembly()
            .GetTypes()
            .Where(x => x.IsInterface && x.Name.EndsWith("Provider"))
    );

foreach (Type provider in providers)
{
    Type temp = provider;

    container.Register
        (
            Component
                .For(temp)
                .UsingFactoryMethod(f => f.Resolve<DictionaryAdapterFactory>().GetAdapter<object>(temp, session))
        );
}

Is there a better way to register these interfaces besides using a loop for?

+3
source share
1 answer

There is no better built-in method in Windsor <3.0

Like Windsor 3, you can do this by using Typesinstead AllTypes.

AllTypes really means all non-abstract classes

Types really means all types.

, , AllTypes , , . , Classes AllTypes, Classses Types , .

+3

Source: https://habr.com/ru/post/1795909/


All Articles