views:

19

answers:

1

Does Autofac have an equivalent to StructureMap's AssertConfigurationIsValid method? Or does configuration validation occur when creating the container?

I believe the AssertConfigurationIsValid method checks that the container can create all the configured services. Obviously it can't pick up more subtle configuration mistakes - related to scope, for instance - but it would be nice to know that we are doing as much as we can to prevent issues that could crop up at runtime.

A: 

I don't think Autofac have validation, and an exception is thrown at runtime if a service cannot be resolved. It would probably be useful but I always tend to write unit tests that validates that my container, or rather my modules, contain those services I expect it to have. Unit testing more than makes up for the lack of container validation.

These tests usually follow the pattern of

  1. Create a container with the module under test
  2. For each required service, test container.IsRegistered
  3. Whenever possible, test container.Resolve
Peter Lillevold
I easily implemented the unit tests you suggested, and now I don't feel too sore that Autofac doesn't have built-in validation. Thanks for your excellent answer.
JulianM
@Serilla - glad to hear that. Good luck!
Peter Lillevold