另一个存储库模式问题:如何处理领域模型投影?
我已经阅读了很多有关存储库模式实现的内容,但仍然不知道应该如何实现实体投影查询?
例如,我有大型且复杂的产品实体,但我只想显示产品的名称和 ID。 我应该在哪里实施这个投影?在我的实体存储库中还是在调用者代码中?
I've read a lot about repository pattern implementation and I still can't find out how should I implement entity projections querying?
For example I have large and complex product entity, but I want to display only product's name and it's id.
Where should i implement this projection? In my entity repository or in a caller code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在存储库中执行此操作。
例如,我的 ProductRepository 界面如下所示:
其中
ProductView
是Product
的简化表示。 (例如仅包含名称和 ID)。获取 ProductView 的投影是必须抽象出来的东西,恕我直言,存储库是一个很好的地方。
I do this in the repository.
For instance, my ProductRepository interface would look like this:
Where
ProductView
is a simplified representation ofProduct
. (Only containing name and id for instance).The projection to get a ProductView, is something that has to be abstracted away, and the repository is a good place for it, imho.
我最近一直在梦想以下模式:
通过这种模式,您可以使用 LINQ 表达式和匿名类动态指定投影,如下所示:
我认为这非常简洁。对于 NHibernate.Linq,实现可能如下所示:
免责声明:谨防上述代码中的错误或不良风格。甚至可能无法编译。这只是我的一点想法。但我认为这个想法应该很清楚。
有什么想法吗?
I've been dreaming up the following pattern recently:
With this pattern, you can for instance specify projections on the fly with LINQ expressions and anonymous classes, like this:
Which I think is pretty neat. With NHibernate.Linq, an implementation might look like this:
DISCLAIMER: Beware of bugs or bad style in above code. Might not even compile. This is just from the tip of my head. But I think the idea should be pretty clear.
Any thoughts?
看看这里,你就会明白为什么这么难。
http ://ayende.com/blog/4784/architecting-in-the-pit-of-doom-the-evils-of-the-repository-abstraction-layer
http://lostechies.com/jimmybogard/2009/09/11/wither-the-repository/
Have a look here and you'll understand why it is difficult.
http://ayende.com/blog/4784/architecting-in-the-pit-of-doom-the-evils-of-the-repository-abstraction-layer
http://lostechies.com/jimmybogard/2009/09/11/wither-the-repository/