Re: Decouple SQL queries from class in OOP design



On 9 Nov 2005 17:35:50 -0800, "Hongyu" <forward@xxxxxxxxxx> wrote:

>To my knowledge, in OOP design it's not good to mix SQL queries with
>application codes in classes, because it limits the usability of the
>classes and bound them to the local database schema.

Indeed, it is not good to mix SQL with business rules in *any*
paradigm. You'd like your business rules to know nothing of SQL.
You'd also like your business rules to know nothing of the relational
schema. The business rules want to use a convenient set of data
structures (or objects).

>But I am not sure
>what the best way is to decouple them. The method that I am using right
>now is to create a hash to store all the SQL statements and prepare
>them in the main program, then I will pass this hash as a variable to
>all the classes that are going to use it. I am not sure whether there
>is any better design. Thanks in advance!

I would not do this. Rather I would create a set classes that know
how to query the database and convert the data into the form that the
business rules would like to see it.



-----
Robert C. Martin (Uncle Bob) | email: unclebob@xxxxxxxxxxxxxxxx
Object Mentor Inc. | blog: www.butunclebob.com
The Agile Transition Experts | web: www.objectmentor.com
800-338-6716


"The aim of science is not to open the door to infinite wisdom,
but to set a limit to infinite error."
-- Bertolt Brecht, Life of Galileo
.



Relevant Pages

  • Re: Sql / Dot Net General Discussion
    ... have some sql skills but they are limited. ... to enforce, in a database, a large number of business rules that are unrelated ...
    (microsoft.public.dotnet.general)
  • Re: Decouple SQL queries from class in OOP design
    ... in OOP design it's not good to mix SQL queries with ... it is not good to mix SQL with business rules in *any* ... You'd like your business rules to know nothing of SQL. ... EMP table and query it directly, but send a message to each and every ...
    (comp.object)
  • Re: Critique of Robert C. Martins "Agile Principles, Patterns, and Practices"
    ... since all the SQL is concentrated in one place rather than spread out and duplicated. ... From my point of view it is significantly less since I can forget about the SQL and Schema while dealing with the business rules and interface. ... The partitioning of the code in the Payroll example is just that, ... There are many reasons why such a separation might be beneficial. ...
    (comp.object)
  • Re: Critique of Robert C. Martins "Agile Principles, Patterns, and Practices"
    ... wrap the SQL. ... forget about the SQL and Schema while dealing with the business rules ... The partitioning of the code in the Payroll example is just that, ... such a separation might be beneficial. ...
    (comp.object)
  • Re: Use a variable number of fields
    ... I think business rules would ideally be enforced throughout the ... I would enforce this rule in front end form, ... Some business rules are too complex to be implemented as SQL ... Some business rules can be implemented as SQL constraints ...
    (microsoft.public.access.tablesdbdesign)