Over and over, I find myself developing WinForm business application screens that have a bunch of text boxes for search criteria, then a search button. These are mapped into an expression using Linq, then passed onto my Linq2Sql layer.
I'd like to create an easy way to "bind" these textboxes to the underlying query using different options like "contains", "startswith", "exactmatch", etc...
What I'm imagining is something like this (note SearchBinderT is imaginary):
SearchBinder<Customer> searchBinder = new SearchBinder<Customer>();
searchBinder.Bind(txtFirstName, a=>a.FirstName, SearchBinderOptions.StarsWith);
searchBinder.Bind(txtLastName, a=>a.LastName, SearchBinderOptions.StarsWith);
searchBinder.Bind(txtTelephone, a=>a.Phone, SearchBinderOptions.Equals);
searchBinder.SetAction(btnSearch, MyMethodThatHandlesTheExpressionTreeAndFillsTheResults);
Then clicking search would automatically generate the expression tree where the textboxes weren't empty and execute the search. But thats only one pattern in my head - there are many more. I'm mainly focused on rapid application development.
- What design pattern(s) would you use for this (or is what I'm thinking good)?
- How would you handle other datatypes (dates/numbers with less than/greater than)