Nick DuBois Nick DuBois -4 years ago 58
PHP Question

Traditional Classes when using the Repository Pattern

I've been working with laravel for a few years now. My traditional approach has been to have a class folder under app and put my data access functions in classes. Keep controllers clean, utilize classes to do the dirty work.

Recently a coworker discovered Repositories, and I've been reading up on them and am confused.

Utilizing a Repository, is there still a place for my app/class/ folder, and "traditional" classes? Is it possible to have a healthy balance? Is there a reason to be all in when it comes to the Repository Design Pattern?

Does Best Practice say anything about this?


Answer Source

The idea with the design pattern is to separate the persistence logic (interfacing with the database and/or cache) from the domain model.

Basically any changes being made to the object "in memory" should be kept in the "traditional" class. The repository will grab or change the data from the database, but if there is anything involving business logic that manipulates the data that does not involve changes to the database (whether it be formatting the data or performing some mathematical operation on it), you can keep that in the domain class.

Best Practices (specifically the Single-Responsibility Principle) encourages the separation of a repository or data-mapping logic from the business logic that can be encapsulated with the object data.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download