zoukankan      html  css  js  c++  java
  • 设计 无状态的类,而不是 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
  • 相关阅读:
    ibatis resultMap 结果集映射
    iabtis初探
    struts2获取请求参数的三种方式及传递给JSP参数的方式
    Struts2的运行机制简介
    Spring AOP面向切面编程的实现
    java 单例模式及getInstance的好处
    It is indirectly referenced from required .class files
    AngularJS学习篇(二)
    AngularJS学习篇(一)
    Flex布局语法
  • 原文地址:https://www.cnblogs.com/oxspirt/p/5955315.html
Copyright © 2011-2022 走看看