[英]How can a table have two nullable FKs, where only one of them has a value?
[英]Multiple nullable Guid and only one of them can have a value
我有一个可以链接到其他三个类或手动生成的 FinancialAccount 类,所以我实现了一个 Enum 保存其链接到的类的类型和三个其他可为空的 Guid 保存该链接类的 Guid
public enum AccountClassificaiton
{
Normal,
Bank,
Contract,
Employee,
}
public Account()
{
...
public Guid Id { get; set; }
public string Name { get; set; }
public AccountClassificaiton AccountClassificaiton { get; set; }
public Guid? ContractId { get; set; }
public Guid? EmployeeId { get; set; }
public Guid? BankAccountId { get; set; }
...
}
我需要一个更好的方法来解决这个问题,因为我不能将这些 Guid 移出这个类,并且这个类和这三个类之间存在一对一的关系,并且这些 Guid 中只有一个应该具有取决于什么的值AccountClassification 这个类有
对不起我的英语,我不是母语人士
我在同一个解决方案上使用了 8 个项目的领域驱动设计模式,所以我很难在没有法律问题的情况下向您展示真正的问题
该项目是财务管理系统的一部分,财务管理系统是庞大的ERP系统的一部分
我会以不同的方式设计它:
public enum AccountClassificaiton
{
Normal,
Bank,
Contract,
Employee,
}
public Account()
{
...
public Guid Id { get; set; }
public string Name { get; set; }
public AccountClassificaiton AccountClassificaiton { get; set; }
public List<Account> LinkedAccounts { get; set; }
...
}
类似的东西。
或者,如果您需要按帐户类型快速查找链接帐户,则更好,您可以使用字典,其中键是 AccountClassificaiton 类型(值为 Account 对象)按类型对链接帐户进行分组:
public Dictionnary<AccountClassificaiton, Account> LinkedAccounts { get; set; }
当然,如果您有多个关联帐户,您可以缩进字典以获得更好的搜索效率:
public Dictionnary<AccountClassificaiton, Dictionnary<Guid, Account>> LinkedAccounts { get; set; }
或者使用自定义相等比较器处理 Guid 和 AccountClassificaiton 类型的复合键: https ://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iequalitycomparer-1?view=net-5.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.