i use service to run most of my logic usually people put in the controller , i used to call the services with facade but most of my services should be tight coupled with model objects
when i use facade the only way to pass model object to service class is as a function argument which can be counterproductive since we may need it in multiple methods or parent class methods
UserController
function doSomething(Model/User $user){
DoSomethingServiceFacade::doIt($user);
}
App/Services/DoSomethingService
class DoSomethingService extends BaseService {
public function doIt(Model/User $user){
$this->checkSomething($user);
$this->baseServiceMethod($user);
}
private function checkSomething( Model/User $user ){
// checking
}
}
i want my service to be tight coupled with the model object so i dont have to pass it around when i call methods inside service
class DoSomethingService extends BaseService {
function __construct(protected Model/User $user){}
public function doIt(){
$this->checkSomething();
$this->baseServiceMethod();
}
private function checkSomething( ){
// checking
}
}
but i cant pass the model object to constructor using facade , so here is simplified version of what i came up with
enum UserServiceLoader:string implements ServiceLoaderContract
{
case DO_SOMETHING = App/Services/Model/DoSomethingService::class ;
case DO_SOMETHING_ELSE = App/Services/Model/DoSomethingElseService::class ;
}
Model/User
class User {
// inside a trait
function serve(ServiceLoaderContract $serviceLoader , ... $args ){
return new $serviceLoader->value($this , ...$args );
}
}
UserController
function doSomething(Model/User $user){
$user->serve(UserServiceLoader::DO_SOMETHING)->doIt();
}
im pretty happy with this , calling services is bit bloated but i like readability and tight coupling it provides ... but i like to ask opinion of a senior developer if there is a reason not to use it