简体   繁体   English

使用实体框架的DDD中的大集合

[英]Big collections in DDD using Entity Framework

I try to use DDD to describe domain model and code first approach on mapping to database tables with entity framework (using fluent api). 我尝试使用DDD来描述域模型和代码优先方法,以使用实体框架(使用流畅的api)映射到数据库表。 For example I have 2 entities: Clubs and Users. 例如,我有2个实体:俱乐部和用户。 Club has many Users. 俱乐部有很多用户。 And I need to add new User to Club. 我需要将新用户添加到Club。

class User 
{
    public string Name { get; set; }
    public Club Club { get; set; }
}

class Club
{
    public string ClubName { get; set; }
    public ICollection<User> Members { get; set; }

    public void AddNewMember(User user)
    {
        //..some logic and checks
        Members.Add(user);
    }
}
  1. So, should I load all users collection in Club to add new and save? 那么,我应该在Club中加载所有用户集合以添加新的并保存吗? Or it's excessively? 还是过度?
  2. What is the best way for modeling entities with big collections? 对具有大集合的实体进行建模的最佳方法是什么? Should I always load them? 我应该一直加载它们吗? Or move then to another separate object? 或然后移至另一个单独的对象?

should I load all users collection in Club to add new and save? 我应该在Club中加载所有用户集合以添加新的并保存吗?

No, you can add a new User to Club.Members and EF will save the new user. 不可以,您可以在Club.Members添加一个新UserClub.Members和EF将保存新用户。 It's not necessary to load the users first. 不必先加载用户。

What is the best way for modeling entities with big collections 对具有大集合的实体进行建模的最佳方法是什么

That doesn't depend on the potential size of collections but on how you plan to access them. 这不取决于集合的潜在大小,而是取决于您计划如何访问它们。 If there are Club s with thousands of Users there's no objection whatsoever if you'd want to query a small subset of users by a query like this: 如果Club拥有成千上万的Users ,那么如果您希望通过这样的查询来查询一小部分用户,则不会有任何异议:

from c in Clubs
from u in c.Users
where c.Type = "Golf" && u.Membership = "Gold"
select u

I always encourage using navigation properties. 我总是鼓励使用导航属性。 An EF entity model is for accessing a database in the first place. EF实体模型首先用于访问数据库。 DDD concerns are secondary. DDD问题是次要的。

Of course you should probably always prevent queries in which all users of a club are loaded, unless you really need them. 当然,除非确实需要,否则您应该始终避免加载俱乐部的所有用户的查询。

Well it probably makes sense, based on what I can tell from your scenario, to use the virtual keyword for your collection and let Entity Framework lazy-load your collection 好吧,根据我从您的情况中可以看出的信息,使用virtual关键字进行集合并让Entity Framework延迟加载您的集合可能很有意义

https://msdn.microsoft.com/en-gb/data/jj574232 https://msdn.microsoft.com/en-gb/data/jj574232

Basically let Entity Framework do the work for you 基本上让Entity Framework为您完成工作

How about introducting a new domain concept 'Member'. 如何引入新的域概念“成员”。 Member could be an aggregate and have his own dedicated repo. 成员可以是一个集合,并且可以拥有自己的专用存储库。 or you simply turn off lazy loading with your original design and add it to its collection. 或者您只需要关闭原始设计的延迟加载并将其添加到其集合中即可。 this all depends on your business rules. 这一切都取决于您的业务规则。

public class User
{
    public Guid UserId { get; set; }
}

public class Member
{
    public Guid ClubId { get; set; }

    public Guid UserId { get; set; }
}

public class Club
{
    public Guid ClubId { get; set; }

    public Member RegisterMember(User user)
    {
        // Check business rules..

        return new Member { UserId = user.Id, ClubId = this.ClubId };
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM