Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DDD、EF、聚合_C#_Entity Framework_Design Patterns_Domain Driven Design - Fatal编程技术网

C# DDD、EF、聚合

C# DDD、EF、聚合,c#,entity-framework,design-patterns,domain-driven-design,C#,Entity Framework,Design Patterns,Domain Driven Design,我首先在试验DDD和EF4.1代码。 我有一个聚合根BlogEntry,它看起来很相似: public class BlogEntry { public long Id { get; set; } public string Title { get; set;} public string Content { get; set; } public DateTime Created { get; set; } public virtual ICollection&l

我首先在试验DDD和EF4.1代码。 我有一个聚合根BlogEntry,它看起来很相似:

public class BlogEntry
{
   public long Id { get; set; }
   public string Title { get; set;}
   public string Content { get; set; }
   public DateTime Created { get; set; }

   public virtual ICollection<BlogEntryComment> Comments { get; set; }
}
foreach(BlogEntry be in blogEntryRepository.GetLatestBlogEntries())
{
    string title = be.Title;
    int amountOfComments = be.Comments.Count();
    // display title, amountOfComments, ...
}
select top 10 * from BlogEntry order by Created desc
不幸的是,实体框架在这里所做的是执行一个查询来获取BlogEntry对象,然后对每个BlogEntry执行一个查询来检索注释的数量

->EF生成的SQL与此类似:

public class BlogEntry
{
   public long Id { get; set; }
   public string Title { get; set;}
   public string Content { get; set; }
   public DateTime Created { get; set; }

   public virtual ICollection<BlogEntryComment> Comments { get; set; }
}
foreach(BlogEntry be in blogEntryRepository.GetLatestBlogEntries())
{
    string title = be.Title;
    int amountOfComments = be.Comments.Count();
    // display title, amountOfComments, ...
}
select top 10 * from BlogEntry order by Created desc
然后10次:

select count(*) from BlogEntryComment where BlogEntry = @blogEntryId
我如何在不急于加载所有评论的情况下防止这种行为,但仍然不针对数据库中的每个博客条目进行查询,但又不冲突任何DDD规则

(我希望EF针对数据库发出如下命令:)


谢谢。

您可以这样做,但它会创建匿名类型

 var p=   from a in db.BlogEntries
        select new {a, a.Comments.Count};
        var k = p.ToList();   
编辑。。 你可以这样做

禁用延迟加载, 将注释计数属性添加到blogEntry域类

  public class BlogEntry
         {
          public int commentCount{
            get
            {
             if(this.comments==null){
               return this._commentCount
             }else{
               return this.Comments.count;

             }
            }
            set{
              this._commentCount=value;
            }


        }
        //other properties...

        }
将新方法添加到存储库中,以获取所有注释计数

var p=   from a in db.BlogEntries
        select new BlogEntry{Id=a.Id,CommentCount= a.Comments.Count , ect..};
        var k = p.ToList();

我会选择更简单、更有效的方法——只需在BlogEntry上添加NumberOfComments属性,在每个注释中增加它并将其持久化。仅仅基于这样一个事实:聚合有责任保持数据的一致性。考虑到仅仅显示数据的请求数量与实际更新的数量,我认为没有理由每次有人想要查看时都计算这个数字

我知道这一点,但这如何适应ddd环境呢。存储库不应该返回实体而不是匿名类型吗?@bes:如果您的域对象上有一个非持久化的
CommentCount
属性,您可以让存储库使用Jayantha建议的查询填充该属性,然后返回一个实体。