何處在域模型中公開特定SQL查詢的結果

[英]Where to expose the results of a specific SQL query in the domain model


Which of these examples would be the best way to expose a collection of Orders for a specific Person that contain a specific Product and why? Or maybe there is a better way to do this alltogether? (Sorry I am new to domain modeling). The Orders list is pulled from the database with an SQL query and turned into a List collection.

以下哪個示例是為包含特定產品的特定人員公開訂單集合的最佳方式?為什么?或者也許有更好的方法來完成這一切? (對不起,我是域建模的新手)。使用SQL查詢從數據庫中提取“訂單”列表,並將其轉換為“列表”集合。

A Person has 1 to many Orders and an Order has 1 to many Products.

一個人有1到多個訂單,訂單有1到多個產品。

1)

class Person
{
    List OrdersContaining(Product p)
    {.....}
}

2)

class Order
{
    List ForPersonContainingProduct(Person person, Product product)
    {.....}
}

2)

class Product
{
    List OrdersFor(Person p)
    {.....}
}

2 个解决方案

#1


I would not expose such a method directly on the domain object itself, which encapsulates the data. Rather, I would use the DAO pattern applied to the Order domain. Which, in essence, is a variation of your #2:

我不會直接在域對象本身上公開這樣的方法,它封裝了數據。相反,我會使用應用於Order域的DAO模式。實質上,這是你的#2的變體:

class OrderDAO {
    List<Order> listByPersonAndProduct(Person person, Product product){
        .....
    }
}

This way, the various patterns of access that you need to add over time are separated from the Order domain object.

這樣,您需要隨時間添加的各種訪問模式與Order域對象分開。

#2


Person could still have a .Orders collection which has all their orders. Then it becomes a question of lazy loading an populating this collection when you know you're going to need it. Something like N/Hibernate helps a lot here.

人仍然可以擁有一個包含所有訂單的.Orders集合。然后,當你知道你需要它時,它會變成一個延遲加載這個集合的問題。像N / Hibernate這樣的東西在這里有很多幫助。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/07/72530706efc3f74f864db38e91d56289.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com