Page 1 of 1

method chaining going too far?

Posted: Sat Jul 11, 2009 8:24 am
by koen.h
Everyone tries to keep things simple. Is your first reaction that this could be an API that is still simple enough?

Code: Select all

 
$a->createB()->where()->get('name')->eq()->str('value');
$a->createB()->where()->creationDate()->between->unix($x, $y);
$a->createB()->where()->getName()->lesserThan->int($value);
 
Working with things makes it easier. So I'm looking for first reactions. Eg wouldn't it scare people away?

Re: method chaining going too far?

Posted: Sat Jul 11, 2009 11:52 am
by alex.barylski
What I find most intereating about chaining, is that if it's done properly the result is a quasi-domain specific language (DSL). That being said, if you don't name the methods correctly it can be a real mess.

DRY KISS is what is important to follow when implementing chaining.

Your code is fairly readable. I implemented something similar for validation and SQL -- it's quite common.

Re: method chaining going too far?

Posted: Sat Jul 11, 2009 7:57 pm
by josh
I would build up a criterion object with a fluent interface, assign the instance to a var and pass it to the where method, having the where method accept a fully formed criterion which is created separately, that way all your commands refer to the same subsystem ( within a given method chain ). I dont think its unnecessarily unreadable though. Take a look at zend, when you use where the condition gets passed as a paramater to the where function, it is not chained on... that is the way to go I think

like $select->from( 'blah' )->where( 'foo', 1 )

For both from and where their "subject" in the sentence is the select itself. In your example depending on the last command we could be talking about the select or the condition of the select, which is implied and not explicit in the API.

or ->where( 'some complex sql' )

or ->where( $someObjrepresentingCriteria )

These kinds of decisions are really easy to make when doing TDD.

Re: method chaining going too far?

Posted: Mon Jul 13, 2009 8:22 pm
by Ollie Saunders
I think what you're trying to implement is called ROE (relational object expressions), if you're planning on using this to interact with a relational database. It was done in Smalltalk some time ago. I've tried to find information on it in the past with little success.

I'm interested in it as an alternative to ORMs but I suspect a full ROE layer would be a pretty big undertaking.
Eg wouldn't it scare people away?
That's a terrible thing to ask. Write what you think will work best; if it does work well, tell people that it works well; if they think it's too different for them it'll be their loss. The question is: Is this an effective API?