好好爱自己!

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

 

 

 

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
posted @   立志做一个好的程序员  阅读(295)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示