Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bcarlso/171969 to your computer and use it in GitHub Desktop.
Save bcarlso/171969 to your computer and use it in GitHub Desktop.
class SomeController {
public override void Process() {
var search = new UserSearch();
search.firstName = htmlForm.get("firstName");
search.lastName = htmlForm.get("lastName");
var renderable = prepareToRender(search.getResults());
// render view.
}
public IList<IDictionary<string, string>> preparetoRender(IList<User> searchResults) {
// Map users to dictionary of strings
}
}
class UserSearch {
public string firstName;
public string lastName;
enum SearchCriteria {
FIRST_NAME = 0x01,
LAST_NAME = 0x02
}
public IList<User> getResults() {
IDictionary<int, FinderDelegate> finders = new DickedInAiry<int, FinderDelegate>();
finder[1] = delegate { repository.findByFirstName(firstName) }
finder[2] = delegate { repository.findByLastName(lastName) }
finder[3] = delegate { repository.findByBoth(firstName, lastName) }
int selector = (firstName != null ? FIRST_NAME : 0) |
(lastName != null ? LAST_NAME : 0);
if(finder.ContainsKey(selector)) {
return finder[selector]();
} else {
throw new ApplicationException("You can't search that way");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment