This is killing me because I feel like I've seen something around before, but my searches are coming up empty and I don't have various patterns committed to memory (yet).
The Situation
I have a search grid that allows users to filter based on multiple attributes. The filters that users would like are passed in via a NameValueCollection and parsed out.
Currently, our legacy app checks for each potential filter in a separate if statement and then runs custom code. And since cyclomatic complexity just isn't my thing, 30 almost identical if statements bug me.
Towards a Better Way
Rather than continuing to check each item in a hard-coded way, I've re-written the parser so that now I can quickly obtain a list of all filters that will be applied. I'm also refactoring the filters themselves into their own classes.
So, I have a list of filters in string form ("Name", "BirthDate", etc.) and a bunch of classes (NameFilter, BirthDateFilter, etc.)
The Question
What is a good, standard way to take a list of strings representing filter names and turn it into a list of those filter objects themselves?
I suppose I could do some sort of FilterLocator with a LocateFilter(string filterName) method, but that seems like it would lead to one long switch statement and seems kind of blech.
I get the sense this is heading towards some sort of IoC/DI solution, but that I don't know enough about either to fully realize it yet.
Thanks for your thoughts!