Entity framework 如何在实体框架导航属性中使用存储库模式?

Entity framework 如何在实体框架导航属性中使用存储库模式?,entity-framework,Entity Framework,假设我们有两个实体Person和Address,因此有两个存储库: public class PersonRepository { Person FindById(object id){...} void Insert(Person entity){...} ..... } 及 PersonRepository的任何用户都可以访问抛出导航属性的人员的地址并对其进行操作,而无需使用AddressRepository: personRepository.FindBy

假设我们有两个实体Person和Address,因此有两个存储库:

public class PersonRepository {
    Person FindById(object id){...}
    void Insert(Person entity){...}
    .....
}    

PersonRepository的任何用户都可以访问抛出导航属性的人员的地址并对其进行操作,而无需使用AddressRepository:

personRepository.FindById(100).Address.Add(newaddress())

这种行为允许吗

一般来说,如何处理存储库模式中的导航属性


谢谢

您可以取消导航属性,我认为存储库模式纯粹主义者会提倡这样或类似的方法,但老实说,这样做会使您的API使用起来更加笨拙。我真的看不出你在这里干什么有什么问题,这与我无关。但是,如果您提供公共属性,您无法控制谁将访问它们。我不知道如何限制这种访问。有很多不同的方法可以塑造使用API的代码。显然,您可以更改API本身,但这并不是100%有效(即使您删除了nav属性,开发人员仍然可以直接设置ID)。或者,如果您的团队正在内部使用您的API,您可以使用StyleCop和/或FxCop创建并强制执行编码标准规则。
public class AddressRepository {
    Address FindById(object id){...}
    void Insert(Address entity){...}
    .....
}