Search code examples
c#dependency-injectioncastle-windsor

Castle Windsor - registration by convention


I have a project that uses Castle Windsor for a DI container.

Initially it was a small project so I have been registering each class individually, however the project is now growing so I would like to register by convention, however I am finding documentation difficult to apply to my situation.

Without going into too much detail I have a project which contains the interfaces each one following a naming convention such as IUserService.cs or IBuildingService.cs.

I then have another project which contains the implementations sticking to a similar convention UserService.cs or BuildingSevice.cs

At the moment that required two separate calls to Kernel.Register.

I know it is possible to register by convention, I am just not familiar with the syntax - anyone able to offer some guidance?


Solution

  • I think what you are looking for is this:

    container.Register(Classes.FromThisAssembly() 
        .WithService.DefaultInterfaces());
    

    If the implementations don't sit in the same assembly as the registration code then use on of the other "FromAssembly" methods

    Castle's documentation is quite good :)