从填充数据层,或查询数据库的直接域模型? [英] Populate the domain model from data layer, or query database direct?
问题描述
说我有一个域模型,其中3个对象进行互动,的预订的汽车的和的舰队的。该舰队有许多车辆,每辆车可以有许多保留。例如
舰队-1 - * - 车辆-1 - * - 预订
如果我想的舰队的有一种方法的 getMostPopularVehicle()的,我可以把它遍历每个车辆和计数预订的数量。
如果我再要引入持久性的ORM,我应该(1)有getMostPopularVehicle()调用数据层方法之前迭代之前填充舰队,车辆及预订?或者我应该(2)现在只需直接查询数据库中的数据层方法来获取最流行的交通工具?
我的想法是,(1)是正确的,但数据库查询可以如此高效。也许我接近这一切都错了吗?
如果这些统计数据预计是现成的,我可能会去的,将避免经常执行一个潜在的重型查询2其他选项之一(如果在所有):
<醇开始=3>Say I have a domain model where 3 objects interact, Reservation, Vehicle and Fleet. The Fleet has many Vehicles, and each Vehicle can have many Reservations. e.g.
Fleet -1--*- Vehicle -1--*- Reservation
If I want Fleet to have a method getMostPopularVehicle(), I could have it iterate each Vehicle and count the number of Reservations.
If I then want to introduce an ORM for persistence, should I (1) have getMostPopularVehicle() call a data layer method to populate the Fleet, Vehicles and Reservations before iterating it as before? Or should I (2) now just query the database directly to get the most popular vehicle in the data layer method?
My thinking is that (1) is correct, but a database query can be so efficient. Perhaps I am approaching this all wrong?
If these statistics were expected to be readily available I'd probably go for one of 2 other options that would avoid executing a potentially heavy duty query as often (if at all):
- Retrieve the data by querying the database directly (avoiding the domain model) using CQRS. The returned view models can be cached to avoid subsequent users causing the query to execute again.
- Create a separate reporting database. Key the values on fleet id and vehicle id and maybe increment the value by 5 for every reservation and then decrement EVERY value by 1 once a week, never going any lower than 0. This way, you can keep fairly representative rolling statistics, whilst being able to quickly query the table and find the highest value row for a given fleet.
这篇关于从填充数据层,或查询数据库的直接域模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!