[英]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.