Castle Windsor - registration by convention
Asked Answered
B

1

6

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?

Bugeye answered 7/7, 2016 at 10:18 Comment(3)
Possible duplicate of Castle Windsor: How to register by convention with delegate method?Farrel
Not really a duplication because his interfaces/classes have no common interface.Discriminate
@Bugeye - Did it solve it for you?Discriminate
D
6

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 :)

Discriminate answered 7/7, 2016 at 10:29 Comment(3)
Slight change in that you need to do .FromThisAssembly().InNamespace().WithService... Other than that exactly what I was looking for :)Bugeye
Good to here :) I left the InNamespace out because didn't want to confuse with maybe not essential stuff. Glad it worked!Discriminate
Yeah, sent me down the right path, I was getting a bit mixed up - so simple in the end! Thanks again!Bugeye

© 2022 - 2024 — McMap. All rights reserved.