Provider Pattern and Unit Testing
I’ve recently wrapped up a project using .NET 2.0 and this project manifested the need to have certain pluggable pieces. For these pieces, I chose to go along with the rest of the .NET 2.0 framework and implemented this using the Provider design pattern.
Following the pattern strictly involves deriving from ProviderBase and then using the ProvidersHelper class to instantiate all the classes from the configuration section.
The ProviderBase class has an Initialize method that gets called by the ProvidersHelper.InstantiateProviders method. The other thing is that you should be creating static methods that delegate to the underlying (previously instantiated) provider.
Does the Provider pattern lend itself well to unit testing? Let’s look at a set of unit testing rules suggested by Michael Feathers. His last rule is that a test is not a test if you have to do special things to your environment (such as editing config files) to run it.
I’m leaning towards thinking that the extra layer (the class that has all the static methods) is not worth the headache. Would it be better if there was a base class or an interface defined, and then you could test the implementation directly?
Sure, you could test the implementation directly now, but to get the information into it now, you’d have to perform additional setup and pass that off to the Initialize method. The principles of dependency inversion state that an object should be given its dependencies, rather than asking for them. Given this, it makes sense that these items go into the constructor of the class.
As our company becomes more familiar with the principles of unit testing, I fear that we will fall into this situation more and more. Given what I know now, I will certainly advise against the Provider pattern in favor of an interface/factory class approach, which will also facilitate integration with a dependecy injection container (such as Castle Windsor).
Are you unit testing providers? Are your experiences similar? Id love to hear about it.
You made a very good point about having to tweak configs to unit test with providers. I’m not testing with providers yet, but I do prefer interfaces/factories.
Ironically, you can implement “provider interfaces” and thus write custom providers, but they are so messy that they are hardly worth the trouble.
Yeah, I’m struggling to figure out how to test with dependency injection when we have a provider pattern (sort of) already implemented enterprise wide. I don’t necessarily want to TEST the provider, but I want to test the DAL classes which USE a provider. Unfortunately, I have found no way to elegantly inject that dependency of the provider, since as you point out, the .NET Provider model used a base class and uses a configuration file to determine the properties of the concrete provider. I don’t want to change the overall architecture, since its implementation is so entrenched. It would take a major project charter to refactor the implementation for testability.


