有关罗布科纳的存储库模式的几个问题? [英] Some Issues about Rob Conery's repository pattern ?

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

问题描述

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

我想申请资料库模式
  作为罗布科纳的描述上他的
  博客下的 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.

罗布提出了自己的模型,并使用了一些
  ORMLinqToSql或EF,以他的数据库映射到
  实体。

Rob made his own "Model" and used some ORM "LinqToSql or EF" to map his database to Entities.

然后,他使用的定制库哪些
  给的IQueryable<基于myModel>
  这些仓库他做了排序
  映射或者ORM之间的分析实体及其模式类。

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之间的实体和我的自定义映射
  模式和负载只是
  性能,我想??
希望
  一点是明确的。

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

**

**

毕竟并且相对于Mr.Rob Conery设备的意见,我已经得到了更好的解决方案为:


  1. 我建我的模型为 POCO 的,并把它们在我的模型层,所以他们一点关系都没有用EDMX文件。

  2. 我建库来处理这个 POCO 依赖于的DbContext
  3. 模型
  4. 然后我创建了一个的ViewModels 得到公正,需要通过从库视图中的信息。

  1. I built my model as "POCOs" and put them in my "Models Layers" so they had nothing to do with "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.

当我看到这个模式比罗布科纳的更好。

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

推荐答案

是的,这是可能的,如果你使用LINQ to 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.

让我们称之为ProductSummaryView:

Let's call it ProductSummaryView:

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

}

现在从回购加载:

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

              }

这将拉动所有的产品,其价格> 100,并返回一个IQueryable。此外,因为你只要求2列,只有两列将在SQL调用中指定。

This will pull all products where the price > 100 and return an IQueryable. In addition, since you're only asking for 2 columns, only two columns will be specified in the SQL call.

这篇关于有关罗布科纳的存储库模式的几个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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