我应该使用什么设计模式在 Linq 搜索屏幕的查询和文本框之间创建简单的绑定映射?
我发现自己一遍又一遍地开发 WinForm 业务应用程序屏幕,其中有一堆用于搜索条件的文本框,然后是一个搜索按钮。 使用 Linq 将它们映射到表达式中,然后传递到我的 Linq2Sql 层。
我想创建一种简单的方法来使用不同的选项(例如“contains”、“startswith”、“exactmatch”等)将这些文本框“绑定”到底层查询...
我想象的是这样的(注意SearchBinderT 是虚构的):
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);
然后点击搜索会自动生成文本框不为空的表达式树并执行搜索。 但这只是我脑海中的一种模式——还有更多。 我主要关注快速应用程序开发。
- 您会为此使用什么设计模式(或者我认为很好)?
- 您将如何处理其他数据类型(小于/大于的日期/数字)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
lambda 的延迟执行和捕获状态不能解决这个问题吗?
由于
searchQuery
在单击按钮之前不会执行,因此无需抽象出与“构建器”组件的比较。 访问文本框存在整个多线程问题,但您可以使用Expression
代替,并使用Control.Invoke
分解和重建。 或者,只需从Invoke
开始编写即可。 或者,将其发送回 UI 线程来运行。但是,如果您确实想遵循您的设计 - 我建议使用
Expression
而不是您拥有的比较运算符。 它更加灵活,并且可以为您节省大量工作。至于设计模式,我知道的最接近的是 查询对象 - [N]Hibernate 的形式为 标准。 不过,它更多地涉及构建动态查询,而不是绑定到 UI 元素(同样,我没有看到这一点)。 在您的示例中,您的查询是硬编码的 - 所以我不确定会有什么好处。
Wouldn't the delayed execution and captured state of lambdas solve this?
Since
searchQuery
isn't executed until the button clicks, there's no need to abstract out the comparisons to "builder" components. There's the whole multithreaded issue with accessing the textboxes, but you could use anExpression<T>
instead and decompose and rebuild withControl.Invoke
. Or, just write it withInvoke
to begin with. Or, send it back to the UI thread to run.But, if you really wanted to go with your design - I'd suggest using
Expression<T>
instead of the comparison operators you have. It's more flexible, and will save you a good bit of work.As for design pattern, the closest I know of would be a Query Object - which [N]Hibernate has in the form of Criteria. It deals more with building dynamic queries though, not binding to the UI elements (which, again, I'm not seeing the point of). In your example, your query is hardcoded - so I'm not sure what the benefit would be.