使用 EF 4 和存储库模式的对象查询模式
关于存储库模式和查询对象模式的问题。我正在使用 EF 4,并使用 VS 2010 中的 ADO.NET POCO 实体生成器从数据库模型生成了 POCO 类。edmx 文件和 tt 文件(POCO 类)位于 2 个不同的项目中。
我的存储库是特定于域的,例如 DocumentRepository 和 UserRepository。我的数据库模型与域模型的不同之处在于我实现了映射器,以便将域对象转换为一个或多个数据库表(反之亦然)。一个例子是我的 Document 域类被建模为数据库中的 3 个表(因此 POCO 类)。
在这种情况下使用域对象时,您将如何实现查询对象模式?在我看来,我必须根据 POCO 类而不是域类编写查询对象?但这不会破坏存储库模式吗?
A question regarding the repository pattern and query object pattern. I'm using EF 4 and have generated my POCO classes from my database model using the ADO.NET POCO Entity Generator in VS 2010. The edmx file and the tt file (POCO classes) are in 2 different projects.
My repositories are domain specific, e.g DocumentRepository and UserRepository. My database model differs from my domain model in such a degree that I have implemented mappers in order to translate a domain object to one or more database tables (and vice versa). One example is that my Document domain class is modeled as 3 tables (and therefore POCO classes) in the database.
How would you implement the query object pattern when using domain objects in such a case? The way I see it I'll have to write the query object base on the POCO classes and not the domain classes? But wouldn't that break the repository pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ORM 通常以直接处理域对象的方式使用=它从数据库加载它们并将它们持久化到数据库。您正在执行另一个抽象步骤,仅使用 ORM 实体来填充自定义对象。您的自定义对象完全超出了 ORM 工具的范围,您不能指望 ORM 工具将为您在域对象之上构建的查询提供任何支持。您必须构建自己的查询支持并将域查询转换为存储库内的 ORM 查询。这通常是通过实施规范模式来完成的。
顺便提一句。在这种情况下,POCO 没有太大意义 - POCO 适用于您想要将它们用作域对象的场景)。
ORM is usually used in the way that it works directly with domain objects = it loads them from database and it persist them to database. You are doing one more abstraction step where you are using ORM entities only to fill your custom objects. Your custom objects are completely out of scope of your ORM tool and you cannot expect that ORM tool will provide you any support for queries build on top of your domain objects. You must built your own query support and translate domain queries to ORM queries inside your repositories. This is usually done by implementing Specification pattern.
Btw. in such scenario POCOs doesn't make too much sense - POCOs are for scenarios where you want to use them as domain objects).