设计 无状态的类,而不是 stateful

作者创建了一个基于PDO的数据库抽象层(DBAL),旨在减少脚本中分散的SQL查询,提高维护性。但在处理多个相同类型的查询时遇到问题,尤其是当一个HTTP请求包含多个SELECT语句时,只有第一个能正常工作。尝试了清空之前查询填充的属性的方法但未成功。社区建议避免使用状态变量,并推荐原子操作的方法来改进。

 

 

 

I have created a Database Abstraction Layer over PDO to refrain from creating multiple queries around my scripts which would be pretty hard to maintain.

My DBAL is not very broad; It takes care of simple tasks such as INSERTING, UPDATING and SELECTING (with or without joining). It does not cover more advanced stuff such as selecting from multiple tables etc.

The problem that raised with my DBAL is that it is confusing queries when there are more of the same type in one HTTP request. For example there are three select statements in my script, the first one works, the other two don't. I tried creating a flush method to clear the previously filled attributes by the query, but it's not working and I'm out of ideas. I'm not ready to get rid of my class and get back to writing queries all over - it's so easy to write them this way.

Anyway, this is how I do some queries with my class:

0down voteaccepted

Don't make it stateful.

Even without looking at the code I'll tell you what's the problem: get rid of $this->stmt variable.
For some reason, all the DBAL writers have strong inclination to such a variable... introducing state to their class and thus making it unusable.

All the method calls have to be atomic, each performing all the necessary operations and returning all the requested data. While saving nothing in the class variables. As simple as that. In such a rare case when PDOStatement object have to be used further - return this very object, don't save it inside. Otherwise just return the requested data.

I wold also advise to get rid of your whole DBAL, as it's written out of good intentions but I can tell for sure that implementation turns out to be less helpful but it actually makes your code worse in many aspects - readability, flexibility, maintainability. In pursue for the fictional usability, you are saving yourself only a word or two from SQL, but making whole application code unreliable.

You won't listen to me, though. Some experience in maintaining applications is required to see my point.

 

 

Please suggest an alternative method to simplify the query writing process without using an ORM or something similar. Is there a good DBAL based on PDO out there? – Aborted Dec 19 '13 at 22:19
2 
There is very little sense in creating a DBAL over PDO as PDO is already one. What's you're after is calledquery builder. You can find a lot of examples googleing for the term. though you don't actually need even this one too. I don't understand that strong desire of all the newbie programmers to get rid of precious SQL. – Your Common Sense Dec 20 '13 at 7:47 
   
Accepting your answer as it includes very useful (and honest) advice. I've gotten rid of my class and start using queries all around again. I'm actually not that much of a newbie programmer, I just don't have enough time to become a professional. – Aborted Dec 20 '13 at 21:26

转载于:https://www.cnblogs.com/oxspirt/p/5955315.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值