In a Laracasts tutorial I see this:
app/controller/TasksController
app/Acme/Services/TaskCreatorService
The TasksController
has this method:
public function store()
{
// ...
$this->TaskCreator->make(Input::all());
// ...
}
And in the TaskCreatorService
the method make
validate and create the Task.
What is this methodology and why Jeffrey Way did it?
Why not create a task directly in the controller?
Sorry for my english
It's actually about Dependency injection which is:
Dependency injection is a software design pattern in which one or more dependencies (or services) are injected, or passed by reference, into a dependent object (or client) and are made part of the client's state. The pattern separates the creation of a client's dependencies from its own behavior, which allows program designs to be loosely coupled and to follow the dependency inversion and single responsibility principles.
In this case it's part of SOLID principles. In SOLID
the Dependency inversion principle
states that one should "Depend upon Abstractions. Do not depend upon concretions."
Advantages of dependency injection:
Because dependency injection doesn't require any change in code behavior it can be applied to legacy code as a refactoring. The result is more independent clients that are easier to unit test in isolation using stubs or mock objects that simulate other objects not under test. This ease of testing is often the first benefit noticed when using dependency injection.
Dependency injection allows a client to remove all knowledge of a concrete implementation that it needs to use. This helps isolate the client from the impact of design changes and defects. It promotes reusability, testability and maintainability.
Dependency injection can be used to externalize a system's configuration details into configuration files allowing the system to be reconfigured without recompilation. Separate configurations can be written for different situations that require different implementations of components. This includes, but is not limited to, testing.
Reduction of boilerplate code in the application objects since all work to initialize or set up dependencies is handled by a provider component.
Dependency injection allows concurrent or independent development. Two developers can independently develop classes that use each other, while only needing to know the interface the classes will communicate through. Plugins are often developed by third party shops that never even talk to the developers who created the product that uses the plugins. (Ref:wikipedia).