ASP.MVC:反映 IQueryable 但不反映 Linq to SQL、DDD 如何提问的存储库 [英] ASP.MVC: Repository that reflects IQueryable but not Linq to SQL, DDD How To question

查看:20
本文介绍了ASP.MVC:反映 IQueryable 但不反映 Linq to SQL、DDD 如何提问的存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 DDD 存储库,该存储库返回与 Linq 到 SQL 基础类匹配的 IQueryable 实体,减去任何关系.我可以轻松地返回实体减去与 Linq select new {field, field, ... } 投影的关系.如何对存储库实体类进行编码?我如何从具有存储库类而不是 Linq 到 SQL 类的存储库中返回一个对象,并且仍然使用 Linq 选择中的多个实体填充它?我将如何在我的 ViewModel 中引用这个返回的类?

I want to create a DDD repository that returns IQueryable Entities that match the Linq to SQL underlying classes, minus any relations. I can easily return Entities minus the relations with a Linq select new {field, field, ... } projection. How do I code the Repository Entity class? How would I return an object from the repository having the repository class not the Linq to SQL class and still fill it with multiple entities from the Linq selection? How would I reference this returned class in my ViewModel?

我对此很陌生,因此有明显的错误.我是否错过了这条船,应该只从存储库中返回完整的实体,而不是投影?在从存储库发送它们之前,我仍然需要去除 Linq 到 SQL 的关系.我完全偏离基地了吗?我真的很想保留 IQueryable 数据类型.

I am very new to this, thus the obvious mistakes. Am I missing the boat and should only return complete Entities from the repository, not a projection? I would still need to strip out the Linq to SQL relations before I send them from the repository. Am I totally off base? I really want to keep the IQueryable data type.

例如,我的存储库中的 Linq to SQL 代码:

For example, my Linq to SQL code in my repository:

public class MiniProduct
{
    public MiniProduct( string ProductNo, string Name, string Description, double Price)
    {    this.ProductNo = ProductNo;
         this.Name = Name;
         this.Description = Description;
         this.Price = Price;
    }
}

public IQueryable<MiniProduct> GetProductsByCategory( string productCategory)
{
    return ( from p in db.Products
             from c in db.Categories
             from pc in db.ProductCategories
             where c.CategoryName == productCategory &&
                   c.CatID == pc.CatID &&
                   pc.ProductID == p.ProductID
             select new { p.ProductNo, p.Name, p.Description, p.Price } );
    // how to return IQueryable<MiniProduct> instead of IQueryable<AnonymousType>???
}

在视图中(尝试强类型 ViewModel)我的模型数据类型是什么以及如何从视图中引用?

And in the View (trying to strongly type ViewModel) what would be my model data type and how to reference from the view?

<% Page Inherits="System.Web.Mvc.ViewPage<MyStore.Models.MiniProduct>" %>

Cottsak 授权代码并使其工作,因此他赢得了复选框.不过,Mark Seemann 指出,这种技术会引起副作用.他说对你的 POCO 的投影或子设置很糟糕.在使代码工作后,我最终制作了更多的实体对象,这会导致不必要的复杂化.最终,我更改了代码以反映 Mark 的建议.

Cottsak empowered the code and made it work, so he earns the checkbox. However, Mark Seemann pointed out that this technique will cause side effects. He was right in that projecting or sub-setting your POCO is bad. After making the code work, I ended up making a ton more one off entity objects, which causes unnecessary complications. Ultimately I changed the code to reflect Mark's suggestions.

补充 Cottsak 的建议:我的存储库返回值是 IQueryable.页面指​​令模型引用类型是

To add to Cottsak's suggestions: My repository return value was IQueryable. The page directive model reference type was

Inherits="System.Web.Mvc.ViewPage<IQueryable<MyStore.Models.MiniProduct>>"

模型字段被访问:

Model.SingleOrDefault().ProductNo
Model.SingleOrDefault().Name
...

这导致了一个

foreach (MyStore.Models.MiniProduct myproduct in Model) {}

谢谢两位的回答.

推荐答案

试试这个:

public class AnnouncementCategory : //...
{
    public int ID { get; set; }
    public string Name { get; set; }
}

.. 并在您的存储库中:

.. and in your repo:

public IQueryable<AnnouncementCategory> GetAnnouncementCategories()
{
    return from ac in this._dc.AnnouncementCategories
           let announcements = this.GetAnnouncementsByCategory(ac.ID)
           select new AnnouncementCategory
           {
               ID = ac.ID,
               Name = ac.Text,
               Announcements = new LazyList<Announcement>(announcements)
           };
}

private IQueryable<Announcement> GetAnnouncementsByCategory(int categoryID)
{
    return GetAnnouncements().Where(a => a.Category.ID == categoryID);
}

这样,我不是投射到匿名类型中,而是投射到我的 AnnouncementCategory 类的新实例中.如果愿意,您可以忽略 GetAnnouncementsByCategory 函数,该函数用于链接每个类别的关联 Announcement 对象的集合,但以某种方式使它们可以延迟加载IQueryable(即,当我 do 最终调用此属性时,我不必调用整个集合.我可以对此执行更多 LINQ 以进行过滤).

This way, instead of projecting into an anonymous type, i'm projecting into a new instance of my AnnouncementCategory class. You can ignore the GetAnnouncementsByCategory function if you like, this is used to chain the collection of associated Announcement objects for each category but in a way so that they can be lazy loaded with IQueryable (ie. so that when i do eventually call on this property, i don't have to call the entire collection. i can do more LINQ on this to filter).

这篇关于ASP.MVC:反映 IQueryable 但不反映 Linq to SQL、DDD 如何提问的存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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