A
A
Andrey Prozorov2018-08-10 10:40:39
PHP
Andrey Prozorov, 2018-08-10 10:40:39

Where do I need to submit another Persistence to the repository by condition?

The repository fetches data using Persistence. There are two Persistence . For example memory and databese. Maybe even memcahed.
Where do you need to insert the condition that if memory Persistence returned 0 then go to databese Persistence?
And even more important, where to cache from db to memory Persistence. Or rather, where it is more correct to divide it from the point of view of OOP.
Approximate code:

class MyController{
    function postsAction(){
        $memoryRepository = new PostsRepository(new PostsMemoryPesistence());

        if( empty(  $posts = $memoryRepository->getAll()  ) ) {
            $posts =  (new PostsRepository(new PostsDBPesistence()))->getAll();
            $memoryRepository->save( $posts )
        }
    }
}

class PostsRepository{
    function __constructor(Presistence presistence){
        this->presistence = presistence;
    }

    function getAll(){
        return this->presistence->getAll();
    }

    function save( Posts[] $posts ){
        foreach($posts as $post){
        this->presistence->save( $post );
        }
    }
}

interface Presistence{
    function getAll();
    functions save();
}

class PostsMemoryPesistence implements Presistence{
    $posts;
    function getAll(){}
    functions save(){}
}


class PostsDBPesistence implements Presistence{
    $db;
    function getAll(){}
    functions save(){}
}

In my opinion, it is necessary to remove the logic of the condition and save from the controller. Where is the question?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmitriy, 2018-08-10
@dmitriylanets

to Service

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question