One way to improve our design is to introduce a new layer in order to limit the responsibilities of our controller layer. We need something to handle the persistence of our media collections and our controller should be able to delegate those operations.
This new layer can apply the service layer design pattern, which is at the heart of service-oriented design.
The idea is that the service layer contains services that handle specific parts of the functionality of an application. For instance, services can take care of data retrieval (for example, fetching data from a remote location), data persistence, data manipulation/transformation, and more.
As applications grow in size and complexity, having a service layer quickly becomes very beneficial as it limits the mental overhead. Additionally, in larger applications, you can even decompose things further by splitting the service layer into two, as follows:
- So-called business services that own the business logic
- Repositories that take care of data persistence/retrieval
We won't go this far for our current application.
You can learn more about the service layer design pattern here: https://martinfowler.com/eaaCatalog/serviceLayer.html.