October 15, 2013

ServiceStack IOC Autowiring by Convention

A situation came up where we wanted to implement a convention to auto-wire many of our classes using ServiceStack IOC. Basically we wanted to eliminate the need to hard-code mappings over and over for every class in a bootstrap file.

For example we wanted to avoid this:


To achieve this we can use .net reflection to scan our assemblies and look for a special interface that represents classes we want to automatically inject. Then we can utilize the ServiceStack IOC method RegisterAutoWiredType to register the types during runtime.

Here's an example where IInjectable is the interface we use to identify which classes will be injected: