Laravel - lots of dependency injection I have the following UserServiceProvider and I'm wondering whether so much dependency injection is overkill:



use
Illuminate\Support\ServiceProvider,
App\Repositories\Eloquent\User\EloquentUserRepository,
App\Repositories\Eloquent\Profile\EloquentProfileRepository,
App\Repositories\Eloquent\Seller\EloquentSellerRepository,
App\Repositories\Eloquent\AppResponse\EloquentAppResponseRepository,
App\Repositories\Eloquent\AppRequest\EloquentAppRequestRepository,
App\Repositories\Eloquent\Model\EloquentModelRepository,
App\Repositories\Eloquent\Maker\EloquentMakerRepository,
Cartalyst\Sentry\Sentry,
App\Services\Cache\Decorators\UserCacheDecorator,
App\Services\Cache\Laravel\LaravelCache,
App\Entities\Entities\Profile\ProfileEntity,
App\Entities\Entities\Seller\SellerEntity,
App\Entities\Entities\AppRequest\AppRequestEntity,
App\Entities\Entities\AppResponse\AppResponseEntity,
App\Entities\Entities\Model\ModelEntity,
App\Entities\Entities\Maker\MakerEntity,
Profile,
Seller,
AppResponse,
AppRequest,
Model,
Maker;

class UserServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this-&gt;app-&gt;bind('App\Repositories\Interfaces\UserRepositoryInterface', function($app)
        {
            $user = new EloquentUserRepository(
                new Sentry(),
                new ProfileEntity(
                    new EloquentProfileRepository(
                        new Profile()
                        )
                    ),
                new SellerEntity(
                    new EloquentSellerRepository(
                        new Seller()
                        )
                    ),
                new AppRequestEntity(
                    new EloquentAppRequestRepository(
                        new AppRequest()
                        )
                    ),
                new AppResponseEntity(
                    new EloquentAppResponseRepository(
                        new AppResponse()
                        )
                    ),
                new ModelEntity(
                    new EloquentModelRepository(
                        new Model()
                        )
                    ),
                new MakerEntity(
                    new EloquentMakerRepository(
                        new Maker()
                        )
                    )
                );
            return new UserCacheDecorator(
                $user,
                new LaravelCache($app['cache'], 'user')
                );
        });
    }
}


I do need all these entities in my UserEntity. Is this normal or should I start reducing the number of injections?
You should reconsider the responsibilities of class UserServiceProvider, too many responsibilities may lead to a lot of dependencies in many cases.
It is quite possible that this class is doing a lot of work that it should not, for example populating User object with sales reports, earnings reports, or activating user, checking activation status etc.  

It is not clear in your question but looking at the names of dependencies it looks like it does a lot of checking and population, you should consider moving respective codes to respective repositories and also refactor the service object into separate classes. This will create a spreaded hierarchy like this:

                           DashboardController
                           /                \
            UserProfileService              UserService
                /        \                    /         \
UserProfileRepository  OtherRepository  UserRepository   SomeOtherDependency


Now, instead of injecting UserProfileRepository, OtherRepository, UserRepository, SomeOtherDependency into UserService and then injecting UserService into controller you can inject UserProfileService and UserService into controller with all of their respective dependencies.

A couple of them looks like they can be defined as a relation. You can, for example, define UserProfile as a relation on User which in a typical case should be a one-to-one relationship. This should sort out the dependencies quite a bit.

Main point to take home is that you should re-evaluate the responsibilities of classes and determine which object should be responsible for what action. as a basic rule of thumb, all data fetching and storing should be a responsibility of repository and all data manipulation and logical decisions should be a responsibility of a service object. If a service object requires more than one repository as a dependency then step back and check if the manipulation of data from second repo should really be a responsibility of service object ?