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

设计 无状态的类,而不是 stateful的更多相关文章

  1. 有状态(Stateful)与无状态(Stateless)

    1.有状态(Stateful): 有数据存储功能.有状态对象(Stateful Bean),就是有实例变量的对象,可以保存数据,类里面有成员变量,而且成员变量是可变的,是非线程安全的.在不同方法调用间 ...

  2. 基于Volley,Gson封装支持JWT无状态安全验证和数据防篡改的GsonRequest网络请求类

    这段时间做新的Android项目的client和和REST API通讯框架架构设计.使用了非常多新技术,终于的方案也相当简洁优雅.client仅仅须要传Java对象,server端返回json字符串, ...

  3. 有状态 无状态 stateful stateless monolithic architecture microservice architecture 单体架构

    为什么游戏公司的server不愿意微服务化? - 知乎 https://www.zhihu.com/question/359630395 我大概说了,方便测试,方便维护,方便升级,服务之间松耦合,可多 ...

  4. SpringCloud系列三:SpringSecurity 安全访问(配置安全验证、服务消费端处理、无状态 Session 配置、定义公共安全配置程序类)

    1.概念:SpringSecurity 安全访问 2.具体内容 所有的 Rest 服务最终都是暴露在公网上的,也就是说如果你的 Rest 服务属于一些你自己公司的私人业务,这样的结果会直接 导致你信息 ...

  5. Date类为什么设计为可变的,而不是像String一样?

    首先,不得不承认,这确实是类库设计的一个错误,所以"为什么"进行了这个错误设计并没有意义.但没有事物一诞生就是完美的,我们的Java只是反应的慢了一点,再慢了一点. 更何况,Dat ...

  6. Flutter - Stateful(有状态) 和 stateless(无状态) widgets

    Stateful(有状态) 和 stateless(无状态) widgets 有些widgets是有状态的, 有些是无状态的 如果用户与widget交互,widget会发生变化,那么它就是有状态的. ...

  7. http协议和web应用有状态和无状态浅析

    http协议和web应用有状态和无状态浅析 (2013-10-14 10:38:06) 转载▼ 标签: it   我们通常说的web应用程序的无状态性的含义是什么呢? 直观的说,“每次的请求都是独立的 ...

  8. 无状态服务(stateless service)

    一.定义 无状态服务(stateless service)对单次请求的处理,不依赖其他请求,也就是说,处理一次请求所需的全部信息,要么都包含在这个请求里,要么可以从外部获取到(比如说数据库),服务器本 ...

  9. 这个知识点不错,,学习一下先。。。无状态服务(stateless service)(转)

    这样的应用,显得高级一些哟~~:) +================== http://kyfxbl.iteye.com/blog/1831869 ========================= ...

随机推荐

  1. phpStorm设置显示代码行号

    File->Settings

  2. Django CRM学员系统项目

    项目需求: 1.分讲师\学员\课程顾问角色,2.学员可以属于多个班级,学员成绩按课程分别统计3.每个班级至少包含一个或多个讲师4.一个学员要有状态转化的过程 ,比如未报名前,报名后,毕业老学员5.客户 ...

  3. linux的学习系列 6---打印文件和发送邮件

    文件打印 如果你希望打印文本文件,最好预先处理一下,包括调整边距.设置行高.设置标题等,这样打印出来的文件更加美观,易于阅读.当然,不处理也可以打印,但是可能会比较丑陋. 大部分的Linux自带了 n ...

  4. Java Networking Related (Java Examples in a Nutshell 3rd Edition)

    Examples to: Use URL class to parse URLs and download the network resources specified by a URL Use U ...

  5. word2010无法打开文件时的一点对策

    word2010无法打开文件时的一点对策 1. Word 安全模式启动,点击「开始」,在搜索栏中输入winword /safe并回车,测试问题是否依然存在? 2. 正常启动Word,点击“文件”—“选 ...

  6. Android中布局文件中使用onClick属性

    安卓开发中,布局文件中的控件有一个属性,是onClick,例如:           <Button             android:id="@+id/button1" ...

  7. XListview刷新和加载

    //继承IXListViewListenerpublic class MainActivity extends Activity implements OnItemClickListener,IXLi ...

  8. 【转载】关于SetWindowOrgEx、SetViewportOrgEx、SetViewportExtEx 和SetWindowExtEx 详解

    关于SetWindowOrgEx.SetViewportOrgEx.SetViewportExtEx 和SetWindowExtEx 详解 1.  SetWindowOrgEx是设置窗口的原点坐标. ...

  9. hdu1501 Zipper

    Zipper Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  10. ios 集合总结

    NSArray 用于对象有序集合(相当于是数组) 它有两个限制: 1. 它只能存储objective-c的对象,但不能存储C中的基本数据类型,如int , float, enum, struct等. ...