Extracted a QueryFactory class.