principlesqlMinor
I want a nice design pattern for "All", "many" or "one"
Viewed 0 times
wantalldesignniceoneformanypattern
Problem
In my particular usage here, I'm using two functions to deal with one thing.
Lets say the user request comes in to select "ALL" the records, in which case I would pass on some code such as:
But then I want to run just one record, I'd do
And if I wanted to get a specific list of records, I'd do something like
It dawns on me that I could of course use getRecordByID everywhere I've used getRecord, but I can't do this for getRecords.
Is there a common or overlooked design pattern I'm missing here? My first guess would be something like checking the argument's contents for a valid array and running based on that, but I'd like to see a better option.
Lets say the user request comes in to select "ALL" the records, in which case I would pass on some code such as:
function getRecords() {
return some_sql("Select * from whatever WHERE id > 0;");
}But then I want to run just one record, I'd do
function getRecords(SomeID) {
return some_sql("Select * from whatever WHERE id = SomeID;");
}And if I wanted to get a specific list of records, I'd do something like
function getRecordsByID(someArray(1,2,3,10,11,99)) {
var someIDs = "";
for (i in someArray) {
someIDs = "(" + someArray.join(",") + ")";
}
return some_sql("Select * from whatever WHERE id IN SomeIDs;");
}It dawns on me that I could of course use getRecordByID everywhere I've used getRecord, but I can't do this for getRecords.
Is there a common or overlooked design pattern I'm missing here? My first guess would be something like checking the argument's contents for a valid array and running based on that, but I'd like to see a better option.
Solution
Why would you want a single method to do three logically different things? Each method you make should really only perform one task. What's the problem in just having the consumer choose the method of selection based upon their need, instead of having a one method fits all sort of thing? If having three methods really isn't what you want, perhaps you could pass a Command pattern object to a database object, to execute the desired functionality?
Context
StackExchange Code Review Q#2800, answer score: 5
Revisions (0)
No revisions yet.