1
votes

I checked many answers and articles before writing this but I wasn't successful. I want to register all the classes that are derived from 1 particular interface IInject

I have an Interface like this:

public interface IFoo : IInject
{
    string GetOrder();
}

To auto register IFoo, I was able to that in Ninject like this:

kernel.Bind(c =>
 c.FromAssembliesMatching("Core.*.dll")
  .SelectAllIncludingAbstractClasses()
  .InheritedFrom<IInject>()
  .BindAllInterfaces());

But in Castle Windsor I could not. If you must know IInject interface is an empty interface only to trigger auto registration:

public interface IInject
{
   //Nothing in this interface
}

How this MATCHING or CONTAINING patter can be used in Castle Windsor?

Thanks,

1

1 Answers

2
votes

The title of your question asks about matching by namespace whereas in the question itself you're asking about a marker interface.

Both are possible with Windsor.

Before providing an answer though I'd strongly suggest reading through Windsor's documentation. There are some significant philosophical differences between Ninject and Windsor and it will help you avoid potential headaches in the future.

Now, back to your question, if you're looking to register by namespace, you'd do something like this:

container.Register(Classes.FromThisAssembly()
    .InSameNamespaceAs<IInject>()
    .WithService.DefaultInterfaces() // or whatever makes sense
    .LifestyleTransient()); // or whatever makes sense

For Registration by marker interface you'd go something like:

container.Register(Classes.FromThisAssembly()
    .BasedOn<IInject>()
    .WithService.DefaultInterfaces() // or whatever makes sense
    .LifestyleTransient()); // or whatever makes sense

Again, don't just copy/paste the code but make sure you fully understand what it does here.