Recently I had a situation where I had a class and interface to make a generic service which would do something based on whatever type is provided. (Obviously, it was not so contrived…)
These would then get injected to other classes to be used as necessary:
Which works fine… as long as you remember to register them.
But what if you make a new service that makes use of IProperty<T> with a <T> that you haven’t used before? Now you have to remember to register that property…. Or do you?
Modules
Remember in this blog post I talked about using Autofac Modules to reduce your headaches and register things automatically. Since we are using constructor injection, and since we’re already looping through the classes in the provided namespace anyway, we can just look at the constructors to grab any instance of IProperty<T> and register it.
Using the reflection techniques I’ve talked about in previous posts, we can create the types that we need to perform the registration. Now we can use our (hopefully useful and not at all contrived) generic type whenever we want and not worry about it being unregistered.