New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RfC / DevCon 2015: Repository layer #81

Open
chriskapp opened this Issue Oct 27, 2015 · 2 comments

Comments

Projects
None yet
2 participants
@chriskapp
Contributor

chriskapp commented Oct 27, 2015

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.

@sidler

This comment has been minimized.

Member

sidler commented Oct 27, 2015

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

@chriskapp

This comment has been minimized.

Contributor

chriskapp commented Oct 28, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment