允许转换为 sql 的 PHP 规范模式
我试图找出在 PHP 中拥有规范模式的最佳方法是什么,其中规范可以(可选)转换为 PHP。
我正在探索一些新的方向并测试它们的效果如何。代码和思路在我脑子里还是很不清楚。
最小接口如下:
interface IRepository {
public function get(ISpecification $specification);
}
interface ISpecification {
public function isSatisfiedBy($candidate);
}
如果存储库隐藏 sql 数据库,则规范需要转换为 sql。添加 ->toSQL() 方法似乎是临时的。翻译规范的类也是一种选择,但最终生成 sql 似乎需要很大的开销。
想法表示赞赏。
I'm trying to find out what the best way would be to have a specification pattern in PHP where the specifications could (optionally) by transformed to PHP.
I am exploring some new directions and am testing how well they would work. Code and ideas are still very unclear in my mind.
Minimal interfaces would be like these:
interface IRepository {
public function get(ISpecification $specification);
}
interface ISpecification {
public function isSatisfiedBy($candidate);
}
If the repository hides a sql database the specification would need to transform to sql. Adding a ->toSQL() method seems ad hoc. A class that translates the specifications is also an option but it seems like a lot of overhead to finally generate the sql.
Ideas appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引用 POEAA(第 324 页):
此描述中的标准当然是您的规范模式。我想说,当应用程序相对较小时,您建议的在条件对象上使用 toSQL 方法的方法是很好的。正如您已经说过的,走其他路线更困难,但它也提供了更大的灵活性和解耦性。最终,只有你能决定。
Quoting from POEAA (pg.324):
The criteria in this descriptions are of course your Specification pattern. I'd say your suggested approach to use a
toSQL
method on the criteria objects is fine when the application is relatively small. Like you already said, going the other routes is more difficult, but it also provides greater flexibility and decoupling. In the end, only you can decide.