kajona/kajonacms

RfC / DevCon 2015: Repository layer

Closed this issue · 2 comments

The model classes have at the moment many tasks. At first they represent a row from the database.
Then they have static methods for data retrieval i.e. getObjectList, getObjectCount and in most
cases they have also methods which contain business logic. It would be great to simplify theses
objects (SRP). So a first step could be to create another "repository" layer which handles all
data retrieval and manipulation. In an action this could look like this:

public function actionList()
{
    $repository = $this->repository->getRepository('class_module_news_news');

    // get all objects
    $news = $repository->getObjectList();

    // get count
    $count = $repository->getObjectCount();

    // get an specific object with the id
    $news = $repository->get($id);

    // update object
    $repository->updateObjectToDb($news);

    // delete object
    $repository->deleteObjectFromDatabase($news);

    // copy object
    $newObject = $repository->copyObject($oldNews, $strNewPrevid);
}

Since the repository would have the same API as the model it would be probably easier to refactor.
Through this we could move a hole bunch of methods from the model into the repository and make the model more like a simple value object. Also we should unify the filter options in each getObjectList/getObjectCount method with a filter object instead of having many arguments.

The repository could be the current orm layer, or do you think of another layer? but separating the models to different objects makes totally sense.
And the unification of getObjectList(Count) is a good idea

Yes thats true and I think we should use the current orm layer. With "another layer" I meant that it should be possible to create a custom repository class which can provide a custom getObjectList/getObjectCount implementation. Maybe we should add a new annotation to the model class which reference the repository class of the model. Doctrine has the same approach to define a custom repository class for the model:
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#custom-repositories