关于Rob Conery的存储库模式的一些问题 [英] Some issues about Rob Conery's repository pattern

查看:100
本文介绍了关于Rob Conery的存储库模式的一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在阅读答案后阅读问题末尾的我的更新:

我正在尝试应用存储库模式 格式为罗伯·科纳里(Rob Conery)的描述他的博客在" MVC店面"下. 但是我想问一些问题 我在应用此设计之前所拥有的 模式.

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制作了自己的模型"并使用了一些 ORM"LINQ to SQL或实体框架(EF)"将其数据库映射到 实体.

Rob made his own "Model" and used some ORM "LINQ to SQL or Entity Framework (EF)" to map his database to Entities.

然后,他使用了自定义存储库 给出IQueryable<myModel>并输入 这些仓库,他做了 在ORM Entities和他的Model类之间进行映射或解析".

Then he used custom Repositories which gives IQueryable<myModel> and in these repositories he made sort of Mapping or "Parsing" between ORM Entities and his Model classes.

我在这里问的是

是否可以在ORM Entities和我的 模型"classes"并仅加载 我想要的属性?我希望 重点很清楚.

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.

为POCO更新

Update For POCO

**

**

毕竟,对于罗伯·科纳里先生的观点,我有以下更好的解决方案:

  1. 我将模型构建为"POCO s",并将其放入模型层"中,因此它们与"edmx"文件无关.
  2. 构建我的存储库以处理依赖于"DbContext"的"POCO"模型
  3. 然后,我创建了一个"ViewModels",以仅从这些存储库中获取视图所需的信息.
  1. I built my model as "POCOs" and put them in my "Models Layers" so they had nothing to do with the "edmx" file.
  2. Built my repositories to deal with this "POCO" model dependent on "DbContext"
  3. Then I created a "ViewModels" to get just the information that needed by view from those repositories.

因此,我 不需要需要在"EF模型"和我的模型"之间再增加一层.我只是稍微扭曲一下模型,然后迫使EF对其进行处理.

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.

如我所见,这种模式比Rob Conery的模式更好.

As I see this pattern is better than Rob Conery's one.

推荐答案

是的,如果您使用的是LINQ toSQL,则有可能.您需要做的就是使用投影将所需的数据提取到您选择的对象中.您不需要接口和其他任何装饰-如果您使用特定于视图的模型(听起来像您需要),请创建一个ViewModel类.

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.

我们称之为ProductSummaryView:

Let's call it ProductSummaryView:

public class ProductSummaryView{
   public string Name {get;set;}
   public decimal Price {get;set;}
}

现在从存储库中加载它:

Now load it from the repository:

var products= from p in _repository.GetAllProducts
              where p.Price > 100
              select new ProductSummaryView {
                  Name=p.ProductName,
                  Price=p.Price

              }

这将拉出价格> 100的所有产品,并返回一个IQueryable.另外,由于您只要求两列,因此在SQL调用中将只指定两列.

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.

这篇关于关于Rob Conery的存储库模式的一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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