nilportugues / php-sql-query-builder

An elegant lightweight and efficient SQL Query Builder with fluid interface SQL syntax supporting bindings and complicated query generation.
http://nilportugues.com
MIT License
417 stars 114 forks source link

Dependency Injection / Code Decoupling #37

Open shadowhand opened 9 years ago

shadowhand commented 9 years ago

Right now the usage of static classes as factories makes it really had to extend the query builder on a per-project basis. A more decoupled approach that would allow for complete dependency injection, would be highly desirable.

Fundamentally, very little needs to change. Primary changes required are:

shadowhand commented 9 years ago

More careful analysis is needed to clearly identify where injections need to happen. If anyone has started research on this, please post your notes here so that effort is not duplicated.

nilportugues commented 9 years ago

I agree factores work nicely, but can be done better.

I was actually thinking of using class maps in this case to reduce memory usage, as no use-statements would be required for starters until the class is actually called upon and an instance is built.

I've been using this approach on other packages successfully and really pays off (eg: https://github.com/nilportugues/validator)

shadowhand commented 9 years ago

The primary problem I see with class mapping is that it often results in a loss of interface type checks. Maybe I've just never seen it implemented properly... even the Validator example is confusing... I'm not sure where $classMap is actually being invoked.

shadowhand commented 9 years ago

Closed? Has this been resolved?

nilportugues commented 9 years ago

Yeah, I rewrote the GenericBuilder in a $classMap fashion. Also Scrutinizer-CI gave the library a better grade.

Changes:

I checked everything else, and there's not much it can be done, I believe... perhaps grouping the other factories together and pull out line 61 to an Abstract Factory.

shadowhand commented 9 years ago

And how would someone inject a modification into that classmap? I don't see a setter function, and the class variable is private, so even if someone extended the class, they would be unable to overload the property...

nilportugues commented 9 years ago

Being core operations I don't intend to make the classMap dynamic...

Tell me about your approach, or a case it would come in handy.

shadowhand commented 9 years ago

@nilopc private variables are effectively saying "i am the smartest guy in the room, no one could ever possibly improve this or use it differently" which every programmer knows is a bogus statement. By using private (as opposed to protected), no one can ever extend the code to do something differently. The entire point of Dependency Inversion is to assist satisfying the Open/Closed Principle. Preventing extension directly violates SOLID programming and makes the software less usable.

shadowhand commented 9 years ago

All of which is a very roundabout way of saying that Query Builder has a long way to go to become extensible. I've not had any time to work on it, but I should have some time over Christmas break to refactor things into a more decoupled library. :)

nilportugues commented 9 years ago

I see your point now!