I have a simple question.
Let’s say I have a .Net solution, with different projects like some class libraries (bll, dal, etc) and a main project which can be a web application or a wpf application, it doesn’t matter.
Now let’s say I want to use an IoC container (like Windsor, Ninject, Unity, etc) to resolve stuff like validators, repositories, common interface implementations and such.
I put it all together. Compiles and runs fine. Then, someday, I add a new service, and in my code I just try to resolve it through the IoC container. Thing is, I forget to register it in the IoC configuration.
Everything compiles, and the application gets deployed and runs. All works fine, except for when a page’s code asks for that new service to the container, and the container answers “hey, I don’t know anything about this service”.
You get your error logged, and the user friendly error page. You’ll go check the error, see the problem and fix it. Pretty standard.
Now let’s say we want to improve the process, and in some way be able to know at compile time if every service that we expect the IoC container to handle is registered correctly in the code.
How could this be achieved? One thing, Unit Tests are ruled out from possible answers, I’m looking for another way, if it does exist.
Thoughts?
EDIT – After some answers and comments, it seems that Unit Tests are indeed the only way to achieve this feature.
What I’d like to know is, if Unit Tests were – for any reason – not possible, and thus IoC could not be tested at compiled time, would this prevent you from using an IoC container and opting for direct instantiation all over your code? I mean, would you consider too unsafe and risky to use IoC and late binding, and see its advantages being outscored by this “flaw”?
It is impossible for the compiler to validate the working of your whole program. The fact that your program compiles, doesn’t mean it works correctly (even without using IoC). For that you’ll need both automated tests and manual testing. This doesn’t mean that you shouldn’t try to let the compiler do as much as it can, but staying away from IoC for that reason is bad, since IoC is meant to keep your application flexible, testable and maintainable. Without IoC you won’t be able to test your code properly, and without any automated tests it is almost impossible to write any reasonably sized maintainable software.
Having the flexibility as IoC provides however, does mean that the dependencies some particular piece of code has, can’t be validated anymore by the compiler. So you need to do this in another way.
Some DI frameworks allow you to verify the container for correctness. Simple Injector for instance, contains a
Verify()method, that will simply iterate over all registrations and resolve an instance for each registration. By calling this method (or using a similar approach) during application startup, you will find out during (developer) testing if something is wrong with the DI configuration and it will prevent the application from starting. You could even do this in a unit test.Important however is, that testing the DI configuration should not need much maintenance. If you must add a unit test for each type that you register to verify the container, you will fail, simply because the missing registration (and thus a missing unit test) will be the reason to fail in the first place.
This gives you ‘almost’ compile-time support. However, you need to be conscious about the design of your application and the way you wire things together. Here are some tips:
NullReferenceExceptions later on. Explicit property injection, where you force the container to inject a property is fine, however, use constructor injection whenever possible.Controllerinstances explicitly in the container. This way the container can check the complete dependency graph starting from the root objects. You should register all root objects in an automated fashion, for instance by using reflection to find all root types. The MVC3 Integration NuGet Package of the Simple Injector for instance, contains aRegisterMvcControllersextension method that will do this for you. Integration packages of other containers contain similar features.Pageclasses for instance, you will probably call the container from within their constructor (sincePageclasses must unfortunately have a default constructor). The key here again is finding them all in once using reflection. By finding all Page classes using reflection and instantiating them, you’ll find out (during app start-up or test time) whether there is a problem with your DI configuration or not.