ASP.MVC: Repository that reflects IQueryable but not Linq to SQL, DDD How To question

前端 未结 2 1960
一个人的身影
一个人的身影 2020-11-29 04:14

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 th

2条回答
  •  被撕碎了的回忆
    2020-11-29 04:45

    Try something like this:

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

    .. and in your repo:

    public IQueryable 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(announcements)
               };
    }
    
    private IQueryable GetAnnouncementsByCategory(int categoryID)
    {
        return GetAnnouncements().Where(a => a.Category.ID == categoryID);
    }
    

    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).

提交回复
热议问题