My Service Interfaces has a namespace of Services.Interfaces
The implementation of the Service Interfaces has a namespace of Web.UI.Services
I have 2 service implementations for example
- IUserService which needs to register to UserService
- ICountryService which needs to register to CountryService
This is how I currently register these services with SimpleInjector.
container.Register<IUserService, UserService> ();
container.Register<ICountryService, CountryService> ();
Problem: If I have over 100 services to exaggerate a bit. I need to go and add a line for each service.
How can I register all the implementations from one assembly to all the interfaces form another assembly using Simple Injector?
You're looking for "Convention over configuration". Simple injector calls this Batch / Automatic registration.
While major DI containers provides API for this, it seems Simple Injector leaves it with us; With bit of reflection and LINQ it is possible to register types as a batch, so Simple Injector provides no special API for this.
Idea is you scan the assembly for concrete types with some convention, looking at whether it implements any interface; if it does, then register it.
Here's the sample pulled from above link:
You can modify this code to apply your convention and register types in batch.
You can do this by querying over the assembly with LINQ and reflection and register all the types:
This is described here.
If this is the case, I would argue that there is something wrong with your design. The fact that you call your services
IUserService
andICountryService
is an indication that you are violating the Single Responsibility, Open/closed and Interface Segregation Principles. This can cause serious maintainability issues.For an alternative design, take a look at the these two articles. The described design allows a much higher level of maintainability, makes it much easier to register your services, and makes applying cross-cutting concerns childs play (especially with Simple Injector).