dimanche 13 septembre 2020

Deal with coupling between two modules - Laravel

How to avoid coupling in the following example: We have two modules:

  • Customers
  • Invoices

The invoice module has an "Invoice" model with a foreign one (belongsTo) to the Customer model of the Customers module.

How can I display a list of a customer's invoices, in the customer edit section, without generating a link? My idea has been to mark a "hook" zone in the customer edit view to be able to inject HTML with a DataTable from the ServiceProvider of the invoice module.

Likewise, in the event that an attempt is made to delete a customer from the Customers module, my intention is to propagate an event so that all those modules that need it can perform an action.

For Eloquent relations I am thinking of using resolveRelationUsing:

Customer::resolveRelationUsing ('invoices', function ($customer) {
    return $customer->hasMany (Invoice::class);
}); 

Is this whole system good practice? Is it a valid way to avoid coupling?

And if, for example, the invoice module had to add a field in the customer model of the customer module ... how should I do it? Maybe with a hook to add the field to the view and an event in the update method of the client controller to know when to update the new field from the invoice module?

My intention is to create modules as independent as possible, to be able to eliminate one of them and keep the application working.

Aucun commentaire:

Enregistrer un commentaire