关于 Rob Conery 存储库模式的一些问题
请在阅读答案后阅读我在问题末尾的更新:
我正在尝试应用存储库模式 正如 Rob Conery 的描述在 他的博客位于“MVC Storefront”下。 但我想请教一些问题 在我应用这个设计之前我有 模式。
罗布制作了自己的“模型”并使用了一些 ORM“LINQ to SQL 或实体框架 (EF)”将其数据库映射到 实体。
然后他使用了自定义存储库 给出
IQueryable
并在 他制作的这些存储库 ORMEntities
与其Model
类之间的映射或“解析”。我在这里问的是:
是否可以在 ORM
Entities
和我的之间进行自定义映射 模型“classes
”并加载 我想要的属性? 我希望 要点很明确。
POCO 更新
**
这是我在多次建议和多次尝试后做出的决定:
**
毕竟,考虑到 Rob Conery 先生的意见,我有更好的解决方案:
- 我将模型构建为“
POCO
s”并将它们放入我的“模型层”中,因此它们与“edmx”文件无关。 - 构建我的存储库来处理依赖于“
DbContext
”的“POCO
”模型, - 然后我创建了一个“
ViewModels
”来获取以下信息:从这些存储库中查看需要的。
因此,我不需要需要在“EF 模型”和“我的模型”之间再添加一层。我只是稍微扭曲我的模型并强制 EF 处理它。
据我所知,这种模式比 Rob Conery 的模式更好。
Please read my update at the end of question after reading the answers:
I'm trying to apply repository pattern
as Rob Conery's described on
his blog under "MVC Storefront".
But I want to ask about some issues
that I had before I apply this design
pattern.Rob made his own "Model" and used some
ORM "LINQ to SQL or Entity Framework (EF)" to map his database to
Entities.Then he used custom Repositories which
givesIQueryable<myModel>
and in
these repositories he made sort of
Mapping or "Parsing" between ORMEntities
and hisModel
classes.What I'm asking here:
Is it possible to make custom mapping between ORM
Entities
and my
model "classes
" and load just
properties that I want? I hope
the point is clear.
Update For POCO
**
This is what I decided after many of suggestions and many of tries:
**
After all and with respect to Mr. Rob Conery's opinion I've got better solution as:
- I built my model as "
POCO
s" and put them in my "Models Layers" so they had nothing to do with the "edmx" file. - Built my repositories to deal with this "
POCO
" model dependent on "DbContext
" - Then I created a "
ViewModels
" to get just the information that needed by view from those repositories.
So I do not need to add one more layer to be between "EF Models" and "My Model". I just twist my model a little and force EF to deal with it.
As I see this pattern is better than Rob Conery's one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,如果您使用 LINQ to SQL,这是可能的。您所需要做的就是使用投影将所需的数据提取到您选择的对象中。您不需要所有这些带有接口之类的装饰 - 如果您使用特定于视图的模型(听起来您需要) - 创建一个 ViewModel 类。
我们称之为 ProductSummaryView:
现在从存储库加载它:
这将拉出价格 > 的所有产品。 100 并返回一个 IQueryable。此外,由于您只要求两列,因此在 SQL 调用中将只指定两列。
Yes, it's possible if you're using LINQ to SQL. All you need to do is use projection to pull out the data you want into an object of your choosing. You don't need all this decoration with interfaces and whatnot - if you use a model specific to a view (which it sounds like you need) - create a ViewModel class.
Let's call it ProductSummaryView:
Now load it from the repository:
This will pull all products where the price > 100 and return an IQueryable. In addition, since you're only asking for two columns, only two columns will be specified in the SQL call.
这并不是回避你的问题,但最终由你决定你的存储库如何工作。
高级前提是您的控制器将指向某个存储库接口,例如
IRepository其中 T : IProduct
。它的实现可以做很多事情——从磁盘加载整个数据库并存储在内存中,然后解析 LINQ 表达式以返回内容。或者它可以仅返回一组固定的虚拟数据以用于测试目的。因为您正在研究存储库接口,所以您可以拥有任意数量的具体实现。现在,如果您正在寻找对 Rob 的具体实现的批评,我不确定这与 StackOverflow 是否有密切关系。
Not a dodge to your question, but it's ultimately up to you to decide how your repository would work.
The high-level premise is that your controller would point to some repository interface, say
IRepository<T> where T : IProduct
. The implementation of which could do any number of things---load up your whole database from disk and store in memory and then parse LINQ expressions to return stuff. Or it could just return a fixed set of dummy data for testing purposes. Because you're banging away on an repository interface, then you could have any number of concrete implementations.Now, if you're looking for a critique of Rob's specific implementation, I'm not sure that's germane to Stack Overflow.
虽然可以使用查询(与存储库模式无关)基于该对象的列子集的查询来填充对象的一部分,但这不是“通常”完成的方式。
如果要返回对象的子集,通常可以创建一个仅包含该属性子集的新类。这通常(在 MVC 世界观中)称为视图模型类。然后,您使用投影查询来填充该新类。
无论您是否使用存储库模式,您都可以执行所有这些操作。我认为这两个概念之间不存在冲突的重叠。
While it's possible to populate part of an object based on a query of a subset of the columns for that object using a query (which has nothing to do with the repository pattern), that's not how things are "normally" done.
If you want to return a subset of an object, you generally create a new class with just that subset of properties. This is often (in the MVC world view) referred to as a View Model class. Then, you use a projection query to fill that new class.
You can do all of that whether you are using the repository pattern or not. I would argue there is no conflicting overlap between the two concepts.
DeferringTheLoad
请记住,IQueryable 将所有加载推迟到最后一个负责时刻。您可能不必使用 LINQ 运算符加载所有数据来获取所需的数据。 ; )
考虑到视图中域类的依赖关系,我会说“不”。为此,请使用 ViewModel 模式。它更易于维护;您可以使用 AutoMapper 来避免映射问题,并且它们在复合视图场景中非常灵活:)
根据对于新问题...答案是肯定的,可以。正如 Rob Conery 所说,使用投影; ):
DeferringTheLoad
Remember that IQueryable defers all the loading up to the last responsible moment. You probably won't have to load all the data using the LINQ operators to get the data you want. ; )
Respecting the dependency in domain classes in views, I will say NO. Use a ViewModel pattern for this. It's more maintainable; you could use AutoMapper to avoid the mapping problems, and they are very flexible in composite views scenarios : )
According to the new question...The answer is yes, you can. Just as Rob Conery says, use projection ; ):