UserDetail 在域和聚合中适合什么位置?
我不确定如何处理 UserDetail 类。
程序;虚拟帐户层次结构,其中 UserAccounts 只能与 MainAccount 进行交易。但用户帐户可以进行某种类型的投注,这将是帐户余额的利润/损失。
将 MainAccount 作为数据库聚合根感觉很合乎逻辑,因为帐户构成了最重要的数据。现在看来,将 UserDetail 作为帐户聚合的一部分似乎没问题,但即使在创建帐户之前创建用户也可能很有用。也许一个用户也可以被允许拥有两个帐户。然后呢?
我猜 UserDetail 可能只是一个独立的聚合。但在这种情况下,在加载帐户聚合时如何加载相应的 UserDetail ?
如果您对此有一些想法,我们将不胜感激。
顺便说一句,我正在使用 FluentNHibernate。
域:
public class MainCashAccount
{
public int Id { get; set; }
public IList<UserCashAccount> UserCashAccounts { get; set; }
public IList<Transaction> UserAccountTransactions { get; set; }
}
public class UserCashAccount
{
public int Id { get; set; }
public UserDetail User { get; set; }
public IList<Bet> Bets { get; set; }
public UserConnection Connection { get; set; } // Not persisted/mapped
}
public class UserDetail
{
public int Id { get; set; }
public string Name { get; set; }
public string LoginName { get; set; }
public string Password { get; set; }
}
其他:
public class UserConnection
{
public TcpClient TcpClient { get; set; }
编辑:
可能的解决方案:(??)
单向
Class UserCashAccountMap
...
HasOne(x => x.User).Cascade.None()
双向定向
Class UserDetail
...(as above)
public UserCashAccount Account {get; set;}
Class UserCashAccountMap
...
HasOne(x => x.User).Cascade.None()
Class UserDetail
HasOne(x => x.Account).Cascade.None().Inverse()
..或者引用而不是 HasOne,以允许每个用户使用多个帐户。
I'm unsure how to deal with the UserDetail class.
Program; A virtual account hierarchy, with UserAccounts which can only have transactions towards a MainAccount. But the UserAccounts can place some type of bets, which will be a profit/loss towards the account balance.
It feels logical to have MainAccount as the database aggregate root, as the accounts form the most important data. As it looks now, it may seem ok to have the UserDetail as part of the account aggregate, but I it could be useful to create a user even before an account has been created. And perhaps a user also may be allowed to have two accounts. Then what?
I guess the UserDetail could be simply a stand-alone aggregate. But in such case, how would I load the respective UserDetail when loading the account aggregate?
Please, if you have some thoughts around this, would be much appreciated.
Btw, I'm using FluentNHibernate.
Domain:
public class MainCashAccount
{
public int Id { get; set; }
public IList<UserCashAccount> UserCashAccounts { get; set; }
public IList<Transaction> UserAccountTransactions { get; set; }
}
public class UserCashAccount
{
public int Id { get; set; }
public UserDetail User { get; set; }
public IList<Bet> Bets { get; set; }
public UserConnection Connection { get; set; } // Not persisted/mapped
}
public class UserDetail
{
public int Id { get; set; }
public string Name { get; set; }
public string LoginName { get; set; }
public string Password { get; set; }
}
Other:
public class UserConnection
{
public TcpClient TcpClient { get; set; }
EDIT:
Possible solutions: (??)
One-Directional
Class UserCashAccountMap
...
HasOne(x => x.User).Cascade.None()
Bi-Directional
Class UserDetail
...(as above)
public UserCashAccount Account {get; set;}
Class UserCashAccountMap
...
HasOne(x => x.User).Cascade.None()
Class UserDetail
HasOne(x => x.Account).Cascade.None().Inverse()
..Or Reference instead of HasOne, for allowing multiple accounts per user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过将 MainAccount 对象的主键作为 UserDetail 对象的外键。
您具体围绕是否有自引用实体的问题,从我引用的您的陈述中,您可以通过多种方式定义您的模型。您可以执行
这 2 项操作或将其任意组合。您还可以选择根本不引用此关系的一侧,并且不建立双向关系。
编辑:回复您的评论。您不想使用
HasOne()
,HasOne 是一种极为罕见的真正一对一的关联类型,几乎没有数据库曾经将关系编码为一对一几乎总是多对一。这是一个References()
关系。在 HasOne 的情况下,两个对象共享相同的主键。这种设计几乎从未在数据库系统中使用过。
需要注意的是,我最初的设计中有一个错误,为了实现双向关联,UserDetails 实际上是 UserAccount 对象上的集合,以避免需要真正的 HasOne 关联。
By having the primary key of the MainAccount object be a foreign key on the UserDetail object.
The question you specifically revolves around to have self referencing entities or not, from your statements I quoted you could define your model in a bunch of ways. You could do
or any combination of these 2. You can also choose to not reference 1 side of this relationship at all and not have a bi-directional relationship.
Edit: Responding to your comment. You do not want to use
HasOne()
, HasOne is an extremely rare type of association that is truly 1-to-1, almost no databases ever have relationships coded as a 1-to-1 they are almost always many-to-1. This is aReferences()
relationship.In the situation of a HasOne both objects share the same primary key. This design is almost never used in database systems.
One note I did originally have an error in my design, to have a bi-directional association the UserDetails would actually be a collection on the UserAccount object to avoid requiring a true HasOne association.