This project is in PHP, but is fairly language agnostic. I’m building a project that has the following MVC format
Main_Class
Main_Class_Common
--> Class_A
--> Class_B
--> Class_C
--> Class_...
--> Class_K
Original requests go to the Main_Class which then includes the appropriate main sub class. The majority of a request will only need to access data in it’s own class. However, there is a small bit of cross-class data. All sub classes need access to shared common functions in Main_Class_Common at all times.
For example,
Main_Class
Main_Class_Common
--> Class_Projects
--> Class_Clients
Usually, Class_Projects never needs data from Class_Clients except in one function it might need to call $class_clients->get_client_details($client_id); and the same occasionally happens in the reverse.
But both classes need to be able to call $main_class_common->clean_input($myinput);.
Currently, I’m using Class class_a extends main_class_common() for access to shared functions which works perfectly. But is the best way to access Class_B from Class_A to just access the global $class_b variable occasionally or should I move the possible shared functions into the Main_Class_Common even though they are used only occasionally?
The project right now is in the early stages of dev, so the current implementation can be changed easily.
The repository pattern would fit in here. Create a
Class_ProjectRepositoryclass andClass_ClientRepositoryclass, that contain methods such asget_client_details($client_id).The MVC controller classes will make use of the repositories. For example, both
Class_ProjectsandClass_Clientscan use theClass_ClientRepositoryto retrieve client details.The repository classes will build the query and call your DAL. Additionally, if you need actual domain objects such as
Class_Clientin your controller, you should also make the repositories responsible for transforming the DAL result sets into domain objects.