【发布时间】:2023-03-21 15:25:02
【问题描述】:
我正在尝试实现存储库模式,我发现的所有示例都非常简单(忽略连接)或使用实体框架。
我的数据库表如下
tblUser {id, fname, lname, email, password, dateAdded}
tblAccount {id, name, isActive, dateAdded}
tblAccountUser {userId, accountId, isActive, dateAdded}
一个用户可以有多个帐户,一个帐户可以有多个用户。 tblUserAccount 有一个布尔值,它告诉我们用户是否对该帐户处于活动状态以及何时添加用户。
我的 pocos 直接映射到具有关系的附加属性的表。有没有更好的方法来完成这部分?我想不出更好的方法让我的存储库返回 GetUsersWithAccounts(userId) 之类的关系。
tblUser {
guid id,
string fname,
string lname,
string email,
string password,
date dateAdded,
IList<tblAccount> accounts
}
tblAccount {
guid id,
string name,
bool isActive,
date dateAdded,
IList<tblUser> users
}
//should i have a tblAccountUser poco with something like
//{tblUser user, tblAccount account, bool isActive, date dateAdded}
我有以下存储库:
UserRepository {
Add()...
...
tblUser GetById(guid userId) {}
IEnumberable<tblUser> GetAll() {}
//was unsure if Account repo should retrieve a user's accounts or if the user repo should.
//using uow.User.GetAccounts(user.id) seems natural but please feel free to let me know what you think
IEnumberable<tblAccount> GetAccounts(guid userId){}
//this is the one i was really unsure about
//this would return tblUser obj with its tblUser.Accounts filled.
IEnumberable<tblUser> GetAllWithAccounts()
}
AccountRepository{
Add()
...
AddUser(guid userId) //only adds relation to tblAccountUser Makes sense?
}
//Should i have a repository for the Account <-> User relations?
问题无处不在,总结一下:
- 从我的存储库返回 pocos 时,我应该如何返回关系。正如我在上面的 pocos 中展示的那样,还是有更好的方法?
- 我的关系表应该有自己的 pocos 吗?如果是这样,是否只有当他们有额外的数据(如 isActive 和用户/帐户特定设置)时。
- 在我的存储库中,我不确定哪个存储库应该处理关系方面的特定请求。
- 我应该为帐户/用户关系创建另一个存储库吗?
欢迎批评、链接、帮助,谢谢。
编辑:
补充说明:应该提到。我想将用户/帐户放在一起的原因是因为它们将位于一个网格中,我们可以在其中激活/停用和修改用户或其帐户的值。
【问题讨论】:
标签: .net repository-pattern data-access-layer