dimanche 8 décembre 2019

What is the best way to instantiate service layer classes?

I've been using laravel for a long time. However, I need better the way I structure my projects.

Sorry for my english, I'm from Brazil.

the structure of my project is: router -> controller -> Service -> Repository -> Model. I place all application logic within the service layer.

My problem is that my services are coupled. And in many situations I'm having circular dependency.

enter image description here

and I use dependency injection to use class instances.

My controllers

class StudentController extends Controller
{
   public function __construct(StudentService $studentService){

        $this->studentService = $studentService;
    }
}

class EnrolmentController extends Controller
{
   public function __construct(EnrolmentService $enrolmentService){

        $this->enrolmentService = $enrolmentService;
    }
}

My services

class StudentService
{
    public function __construct(StudentService $studentService){

       $this->studentService = $studentService;
    }
}

class EnrolmentService
{
    public function __construct(Enrolment $enrolmentBService){

       $this->enrolmentBService = $enrolmentBService;
    }
}

In my example, I need to create an enrollment when registering a student.

You need to change student data when editing an enrollment. The problem is that since one class depends on another, I have a circular dependency problem. I know I should create a third class to try to work around the problem. What I want to know is if this structure of mine is correct. How could I improve this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire