从填充数据层,或查询数据库的直接域模型? [英] Populate the domain model from data layer, or query database direct?

查看:154
本文介绍了从填充数据层,或查询数据库的直接域模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个域模型,其中3个对象进行互动,的预订汽车的和的舰队的。该舰队有许多车辆,每辆车可以有许多保留。例如

 舰队-1  -  *  - 车辆-1  -  *  - 预订

如果我想的舰队的有一种方法的 getMostPopularVehicle()的,我可以把它遍历每个车辆和计数预订的数量。

如果我再要引入持久性的ORM,我应该(1)有getMostPopularVehicle()调用数据层方法之前迭代之前填充舰队,车辆及预订?或者我应该(2)现在只需直接查询数据库中的数据层方法来获取最流行的交通工具?

我的想法是,(1)是正确的,但数据库查询可以如此高效。也许我接近这一切都错了吗?


解决方案

如果这些统计数据预计是现成的,我可能会去的,将避免经常执行一个潜在的重型查询2其他选项之一(如果在所有):

<醇开始=3>
  • 使用 CQRS <直接查询数据库(避免域模型)检索数据/ A>。返回的视图模型可以被缓存以避免后续用户造成查询再次执行。

  • 创建一个单独的报告数据库。重点对车队的ID和车辆ID,也许值5为每一位预约递增值,然后减1的每一个值,每周一次,永远不会比任何0低通过这种方式,你可以保持相当重presentative滚动统计数据,同时能够快速查询表,找到的最高值排一个给定的车队。

  • 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):

    1. 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.
    2. 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆