Laravel : Is there a way to convert to Dependency Injection

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);
    }
}

Answers


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.


Need Your Help

Slow operations in the Model-View-Presenter pattern

winforms model-view-controller user-interface swt mvp

How do you handle slow operations in the Model-View-Presenter (or MVC or M-V-VM or whatever variant you are using)?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.