Is it best practise to call container.Verify()
in production code? I was thinking of moving to a:
#IF Debug
container.Verify();
#ENDIF
I don't have any real reason to make the change, just curious as to what the general consensus/best practise is.
Simple Injector is a free, fast, and flexible inversion of control library that is easy to use and configure. It supports . NET Core, Xamarin, Mono, and Universal apps and is easily integrated with Web API, MVC, WCF, ASP.NET Core, etc.
Simple Injector is thread-safe and its lock-free design allows it to scale linearly with the number of available processors and threads.
Whether or not it is useful to call Verify
is open for debate. Back in 2011, Mark Seemann did think such a method is close to worthless. My opinion is that that there is real value in calling Verify
, but I understand Mark's sentiment and agree that calling Verify
by itself is typically not enough. That's why there's clear guidance on Verifying the container’s configuration in the Simple Injector wiki about keeping your DI configuration verifiable.
With Simple Injector however, Verify
does much more than testing whether it can create your object graphs. A call to Verify
will kick-off Simple Injector's Diagnostic Services which search for very common, but sometimes very hard to spot configuration errors (a feature which was not available during the time that Mark wrote that article).
In general my advice is to call container.Verify
in your production code, for as long as feasible. Call it always at start-up, both in debug and release builds, in your staging environment and on production.
As the application gets bigger, it becomes more time consuming to call container.Verify
during start-up. Some types of applications are more sensitive to this than others. For server applications it is usually okay to have a longer start-up time, while desktop and mobile phone applications must start more quickly.
Once you get into the position that a call to Verify
takes too much time -but only then- you should remove the call to Verify
, but at least still have a unit/integration test that calls container.Verify
. I see no problem wrapping Verify
in compiler directives (as you did in your question), but again note that IMO you should postpone the removal of a call to Verify
in your startup path for as long as possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With