可以使用存储库模式 +存储过程?他们应该/可以返回 IQueryable 吗?
我非常喜欢使用 Repository 模式
返回 IQueryable
对象。然后,我让我的服务层确定做什么(例如,按 XXX 过滤、按 YYY 排序、投影到 ABCD 等)。
但我有一些核心数据库的东西,所以我把它们全部包装到一个存储过程
中。工作正常。我知道 EF 可以执行存储过程..但我不确定这如何适合存储库模式数据层。
有人有任何示例/建议吗?我是否让存储库方法执行存储过程,然后返回结果(例如。ICollection
..这样服务层就只查询该结果?
I'm a big fan of using the Repository pattern
to return IQueryable<T>
objects. I then let my services layer determine what does what (eg. filter by XXX, order by YYY, project into ABCD, etc).
But I've got some hardcore DB stuff, so I've got it all wrapped up into a Stored Procedure
. Works fine. I know EF can execute stored procs .. but I'm not sure how this fits into a Repository Pattern data layer.
Does anyone have any examples / suggestions? Do i let the repository method execute the stored procedure, and then i return the result (eg. ICollection<Foo> as AsQueryable
.. so the service layer then just queries on that result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议继续这样做。在 EF 模型中设置存储过程,返回它需要的任何实体,然后在存储库中创建一个 get 来解决存储过程正在使用的内容。返回数据方式的差异发生在 DAL(在本例中为 EF 模型)内部,您的存储库仍在访问上下文以查找和返回数据。
我现在正在做类似的事情,这是我能想到的最好的解决方案。它允许我继续在 EF 模型上进行数据访问,并使我的存储库与模型收集数据的方式分开。存储库仍然不知道模型如何获取数据,并且应用程序的其余部分不会看到存储库功能的任何不同之处。
I would suggest going ahead and doing just that. Setup the stored procedure in the EF model, returning whichever entity it needs to, then in your repository create a get that addresses what the stored procedure is using. The difference in how the data is returned is occurring inside your DAL (in this case the EF model), your repository is still accessing a context to find and return data.
I am doing something similar right now and that was the best solution I could come up with. It allowed me to continue doing my data access at the EF Model and keep my repository separated from the way the model is collecting the data. The repository is still clueless about how the model gets the data and the rest of the app won't see anything different about the repository functions.