callumthomson's avatar

Performance implications of binding lots of classes to the service container

Hi all,

I'm attempting to make my first scalable Laravel application utilising single action service classes. They are in the App\Services namespace and each class carries out a single action.

For example, here is my LoginController

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\User\Auth\ { LoginUserServiceInterface, LogoutUserServiceInterface };


class LoginController extends Controller
{
    public function login(Request $request, LoginUserServiceInterface $loginUser)
    {
        return $loginUser->do($request);
    }

    public function logout(Request $request, LogoutUserServiceInterface $logoutUser)
    {
        return $logoutUser->do($request);
    }
}

My question is, is this a good practice, considering that I am binding each service class to the service container? I am worried about how this will affect performance, as there could end up being hundreds of bindings.

For example, here is my ServiceServiceProvider as it currently stands:

public function register()
    {
       $this->app->bind(
            'App\Services\Category\CategoryServiceInterface',
            'App\Services\Category\CategoryService'
        );
       $this->app->bind(
            'App\Services\Product\ProductServiceInterface',
            'App\Services\Product\ProductService'
        );
       $this->app->bind(
            'App\Services\User\UserServiceInterface',
            'App\Services\User\UserService'
        );
           $this->app->bind(
                'App\Services\User\Auth\LoginUserServiceInterface',
                'App\Services\User\Auth\LoginUserService'
            );
           $this->app->bind(
                'App\Services\User\Auth\LogoutUserServiceInterface',
                'App\Services\User\Auth\LogoutUserService'
            );
           $this->app->bind(
                'App\Services\User\Auth\RegisterUserServiceInterface',
                'App\Services\User\Auth\RegisterUserService'
            );
           $this->app->bind(
                'App\Services\User\Basket\AddItemServiceInterface',
                'App\Services\User\Basket\AddItemService'
            );
           $this->app->bind(
                'App\Services\User\Basket\DeleteItemServiceInterface',
                'App\Services\User\Basket\DeleteItemService'
            );
           $this->app->bind(
                'App\Services\User\Basket\UpdateItemServiceInterface',
                'App\Services\User\Basket\UpdateItemService'
            );
    }
0 likes
1 reply

Please or to participate in this conversation.