Pregunta

Let's say the context is a Job Portal website.

I have a requirement which says something like this :

When a JobSeeker views a Vacancy, the Vacancy's view count is incremented

How do I represent this in a web?

As far as I can think, it depends on which controller that requested the Vacancy, perhaps something like this:

class VacancyController {
    public function viewVacancy($vacancyId) {
        $vacancy = $this->repo->find($vacancyId);

        $vacancy->incrementView();

        $this->repo->persist($vacancy);

        return $vacancy;
    }
}

Is there a way to represent such rule in the Domain Layer?

¿Fue útil?

Solución

Update view count synchronously is not a good idea, especially when using RDBMS database.

update vacancy_view set 
    count = count + 1
where vacancy_id = $id

The row may get blocked if a lot of users view same vacancy within a short period of time.

I'd suggest use "inserting" instead. The inserting could be synchronous or asynchronous, depends on benchmark. But this implementation details could be hidden behind the applicationEvents abstraction. The view count could be sum up whenever it is requested or cached, in this use case, eventual consistency is usually tolerated.

class VacancyController {
    public function viewVacancy($vacancyId) {
        $vacancy = $this->repo->find($vacancyId);

        $this->applicationEvents->raise(VacancyViewedEvent($vacancyId))

        return $vacancy;
    }
}

It is said some nosql databases(such as Cassandra) fit well in view counting scenario, but I don't have experience with that yet.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top