mardi 1 mars 2016

Laravel - Service Clases for Complex ManyToMany Relationships

I'm working on an application where Model A has several different Many To Many relationships with Model B. Maybe about 10. So what this means is, there are 10 different pivot tables which join Models A and B, depending on the circumstances under which the models are being used.

Each of these 'pivot relationships' is loaded with all sort of functionality that is very specific to that relationship. As such, storing that functionality on Model A or Model B doesn't make much sense, because then those models will be bloated with the functionality of all 10 of those relationships between them.

So I was considering making individual service classes to handle the relationship-specific functionality. For example:

class ModelA {

    public function relationship1() {
       return $this->belongsToMany('App\Models\ModelB', 'relationship_1_table')
    }

    public function relationship2() {
       return $this->belongsToMany('App\Models\ModelB', 'relationship_2_table')
    }


    public function relationship3() {
      return $this->belongsToMany('App\Models\ModelB', 'relationship_3_table')
    }

}

class ModelB {

    public function relationship1() {
       return $this->belongsToMany('App\Models\ModelA', 'relationship_1_table')
    }

    public function relationship2() {
       return $this->belongsToMany('App\Models\ModelA', 'relationship_2_table')
    }


    public function relationship3() {
      return $this->belongsToMany('App\Models\ModelA', 'relationship_3_table')
    }

}

And then having:

class Relationship1Service {

   // methods / properties specific to that relationship

}

class Relationship2Service {

 // methods / properties specific to that relationship

}

class Relationship3Service {

   // methods / properties specific to that relationship

}

The problem I'm facing is when all of these services need to interact and request data from eachother's relationships. If the functionality were stored on each respective Model (rather than service classes), I could say things like:

class Model A {

   public function someDataRetrieval() {

      $this->modelBRelationship()->where()....
   }
}

With the Service classes, I'm finding myself doing things like this:

class Relationship1Service {

   public function setModel(ModelA $model, Relationship2Service $relation2) {
      $this->modelA - $model;
      $this->relationship2Service = $relation2;
      $this->relationshipService2->model = modelA;
      // etc for every Service Relationship I may need
   }

   public function workWithSomeDataRequirngOtherRelation()
   {
      $this->relationship2Service->doSomethingSpecificToYou();    

   }
}

Obviously, this doesn't seem very good, because I need to specify the specific Model instance that forms the basis of each relationship, for every Service. And of course, I want to be able to use each Service independently of one another, and co-dependently when necessary.

Aucun commentaire:

Enregistrer un commentaire