简体   繁体   English

如何使用域驱动设计表示命中数

[英]How to represent hit count with Domain Driven Design

Let's say the context is a Job Portal website. 假设上下文是Job Portal网站。

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? 有没有办法在域层中表示这样的规则?

Update view count synchronously is not a good idea, especially when using RDBMS database. 同步更新视图计数不是一个好主意,尤其是在使用RDBMS数据库时。

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. 但是这个实现细节可能隐藏在applicationEvents抽象背后。 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. 据说一些nosql数据库(如Cassandra)非常适合视图计数场景,但我还没有相关经验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM