Search code examples
c#inversion-of-controlcastle-windsor

How to batch register interfaces without BasedOn method in Castle Windsor?


I’ve got a project under .NET Core. I want to register all repository interfaces to classes that implemented these interface without extending all interfaces from a base interface using Castle Windsor

I don’t want to use the code below:

container.Register(
FromAssemblyContaining<StudentRepository>()
    .BasedOn<IBase>()
    .WithService.Select((type, types) =>
        type.BaseType != null && type.Name.EndsWith(type.BaseType.Name)
            ? new[] { type.BaseType }
            : Enumerable.Empty<Type>()));

I want Castle Windsor to automatically detect all interfaces from the assembly.


Solution

  • Using this code:

    container.Kernel.Register(
        Classes.FromAssembly(typeof(StudentRepository).Assembly)
            .Where(Component.IsInNamespace(typeof(StudentRepository).Namespace, includeSubnamespaces: true))
            .WithServiceAllInterfaces()
            .LifestyleScoped());