Reputation: 1830
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.
Upvotes: 7
Views: 2708
Reputation: 172826
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.
Upvotes: 10