2

I am building a search function.

Speed is priority to getting the result from query so, I use native SQL.

Result is depending on user/group permission, keyword, and the option of search (name, description, all, etc).

The option is extendable so, it might be the best to use Dependency Injection, but I just can't figure out how to use it with this requirement.

I know "if" statement will work just fine. However, I am trying to follow the "single responsibility principle" and come out with the code below.

It is currently working just fine but it just doesn't looks quite right. Is there a way to convert it to Dependency Injection? and maybe you can help to explain why the code below is already good or it is just bad and give me a solution.

Interface :

interface SearchQueryInterface
{
    public function searchQuery($user_id,$user_group, $keyword);      
}

Classes that implements Interface :

// search by All
class SearchAll implements SearchQueryInterface{

    public function searchQuery($user_id,$user_group, $keyword)
    {
        // results = Select query based on $user_id, $user_group and $keyword
        // return query results;
    }
}

// search by Name
class SearchName implements SearchQueryInterface{

    public function searchQuery($user_id,$user_group, $keyword)
    {
        // results = Select query based on $user_id, $user_group and $keyword
        // return query results;
    }
}

// search by Description
class SearchDescription implements SearchQueryInterface{

    public function searchQuery($user_id,$user_group, $keyword)
    {
        // results = Select query based on $user_id, $user_group and $keyword
        // return query results;
    }
}

Controller :

class SearchController extends \BaseController {   

    public function postSearch()
    {
        $user_id = 2;
        $user_group = 1;

        $option = Input::get('search_option');
        $keyword = '%'. Input::get('search_keyword') .'%';

        // get the class based on option
        $search_query = App::make('Search'. $option );
        $results = $search_query->searchQuery($user_id,$user_group, $keyword);

        var_dump($results);
    }
}
2
  • I'm guessing because of this sentence: $keyword = '%'. Input::get('search_keyword') .'%'; that your sql is not a Prepared statement and is sql injectable. Maybe i'm wrong but be carefull with this. Talking about your code i think that you can simplify it with a single interface for all querys and different implementations based on group or wharever you want like SearchQueryAdmins, SearchQueryGuests and so on... Commented Jun 30, 2014 at 14:15
  • @CarlosGoce, Thanks for the warning. I have made it to prevent sql Injection. The way I am separating based on the option is because the extendable one is the option. It will always based on user, group and keyword. I was putting the option inside "if" statement before. But After watching laracast and reading Taylor Otwell book about single responsibility, dependency inversion, etc.. , I think it might be better for unit testing later on. However, now I confuse myself with the Dependency Injection. Commented Jun 30, 2014 at 22:58

1 Answer 1

1

Your question is quite funny. In the first sentence you said "I am building a search function." and then you come up with lots and lots of code related to objects - and not function.

In fact you are asking philosophy and not facts here. So I guess, nobody in this forum will be able to give you a definite process or methodology. So you touch a couple of questions, a group of experts can discuss for years. Let me strive some of them:

  1. Do I need Objects in Web Applications? The yes-sayers argue with clarity of code and a oop as common sense pattern (as their way of thinking is objects). The no sayers will pinpoint you on the nature of the http protocal which strongly - in fact only - supports an import-process-output pattern well (which can be addressed by oop, but where oop does not add value over functional programming).

  2. Where do you need to apply the MVC-pattern? The MVC-pros take this as overarching lifetime philosophy, whereby those people would not even drink a cup of coffee without being able to tell how the process and all of the aquired assets fit into MVC. Then there are people that tend to scope MVC into any direction, whereby two people will obviously will have different definitions of MVC. The third group of people would be able to successfully question every aspect of MVC.

  3. How do I map my objects into databases? This is a nice anti-pattern in itself. One major benefit of relational databases is, that all foreign keys which relate to the same primary key also relate to each other. Objects to objects just store pointers. Concluding data from one pointer containing object to another requires to have more pointers. All that have to be maintained. On that the object heros invented all sort of performance eating gimicks such as object-to-db-mapping, which obviously and luckily the given example code pieces do reject to make use of.

Well I guess I could make much more about extends and the 'class'-keyword and the object notation in php and name spaces and dependency injection and all those things. However I am afraid, that I already started to put some notion of frustration to my words. Let's not go sad and let's stop here.

My only advice is: If you want to build a search function just do so and delete all this overhead, which makes your function an object.

Sign up to request clarification or add additional context in comments.

5 Comments

thank you for the explanation. Sorry to confuse you about the "search function". I was trying to tell everyone that it was at first only have 1 function which is inside the controller "SearchController" method called "postSearch". The query and everything was handled inside "if" statement. After reading Taylor Otwell books and watching Laracast, I try to build the function based on what's told (single responsibility, dependency inversion, etc..). I couldn't figure out how to convert the code to Dependency Injection
My comments inside all the search methods might be confusing as well, so I edited it a lil bit. Yes, I know that it will be many arguments for the question. So you are saying to be best to just go back to "if" statement?
That is at least what would make your code better readable... Actually do what you are confident with. As said earlier for web apps there are very rare use cases, where oop beats functional programming in terms of benefits. So if you need to put a lot of extra effort in transforming functional programming into oop you may waste lots of time. On the other hand if you always do oop you may be more efficient in that area of practice. In that case I would NOT advice you to switch over to functional programming.
There is one nice extra aspect. You said "Speed is priority". As your data is stored in a database your main gain in speed is optimizing at that end. So if you have got 2 days and you are free to decide if using that to transform functional code into oop-code and make that pattern solid VS to use it for database optimization I would clearly vote for the database optimization.
I like your extra aspect. Well, I did the database optimization that I can get thousand of result instantly. I just got a lil bit of free time so I think its time for me to tidy up my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.