使用域模型和POCO类时,查询要去哪里? [英] When working with domain models and POCO classes, where do queries go?

查看:86
本文介绍了使用域模型和POCO类时,查询要去哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是域模型,POCO和DDD的新手,所以我仍在设法了解一些想法.

I am new to domain models, POCO and DDD, so I am still trying to get my head around a few ideas.

我还无法弄清的一件事是如何使域模型保持简单且与存储无关,但仍然能够以丰富的方式对其数据执行一些查询.

One of the things I could not figure out yet is how to keep my domain models simple and storage-agnostic but still capable of performing some queries over its data in a rich way.

例如,假设我有一个实体Order,其中包含OrdemItems的集合.无论出于何种原因,我都想获得最便宜的订单商品,或者可能是当前没有库存的订单商品清单.我不想做的是从存储中检索所有订单项目,然后进行筛选(太昂贵),因此我想以某种方式最终获得类型为"SELECT .. WHERE ITEM.INSTOCK = FALSE"的数据库查询.我不想在我的实体中使用该SQL查询,或者是否希望将其绑定到特定平台(例如Linq2SQL上的NHibernate查询)的任何变体.在这种情况下,常见的解决方案是什么?

For instance, suppose that I have an entity Order that has a collection of OrdemItems. I want to get the cheapest order item, for whatever reason, or maybe a list of order items that are not currently in stock. What I don't want to do is to retrieve all order items from storage and filter later (too expensive) so I want to end up having a db query of the type "SELECT .. WHERE ITEM.INSTOCK=FALSE" somehow. I don't want to have that SQL query in my entity, or any variation of if that would tie me into a specific platform, like NHibernate queries on Linq2SQL. What is the common solution in that case?

推荐答案

域对象应该独立于存储,您应该使用Repostiory模式或DAO来持久化对象.这样,您就可以强制分离关注点,对象本身不应该知道其存储方式.

Domain objects should be independent of storage, you should use the Repostiory pattern, or DAO to persist the objects. That way you are enforcing separation of concerns, the object itself should not know about how it is stored.

理想情况下,将查询结构放入存储库中是个好主意,尽管我会在其中使用ORM.

Ideally, it would be a good idea to put query construction inside of the repository, though I would use an ORM inside there.

这是Martin Fowler对存储库模式的定义.

Here's Martin Fowler's definition of the Repository Pattern.

这篇关于使用域模型和POCO类时,查询要去哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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