简体   繁体   English

多个可为空的 Guid,其中只有一个可以有值

[英]Multiple nullable Guid and only one of them can have a value

I have a FinancialAccount Class that can be linked into three other classes, or manually generated , So I implemented one Enum holding the the type of class that its linked to and three other nullable Guid's to hold the Guid of that linked class我有一个可以链接到其他三个类或手动生成的 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; }
    ...
}

I need a better approach for this problem considering that i cant move these Guids out of this class , and there is a relation of one to one between this class and those three classes , and only one of these Guid should have a value depending on what AccountClassification this class has我需要一个更好的方法来解决这个问题,因为我不能将这些 Guid 移出这个类,并且这个类和这三个类之间存在一对一的关系,并且这些 Guid 中只有一个应该具有取决于什么的值AccountClassification 这个类有

Sorry for my English, I'm not a native speaker对不起我的英语,我不是母语人士

Edit-1编辑-1

Im using a Domain-Driven-Design pattern with 8 projects on the same solution so its not easy for me to show you the real problem without having a legal problem我在同一个解决方案上使用了 8 个项目的领域驱动设计模式,所以我很难在没有法律问题的情况下向您展示真正的问题

Edit-2编辑-2

This project is a part of Financial Management system which is a part of a huge ERP system该项目是财务管理系统的一部分,财务管理系统是庞大的ERP系统的一部分

I would design it differently:我会以不同的方式设计它:

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; }
    ...
}

Something like that.类似的东西。

Or, even better in case you need to find linked accounts fast by the account type, you can use a Dictionary where the key is the AccountClassificaiton type (value is the Account object) to group the linked accounts by type:或者,如果您需要按帐户类型快速查找链接帐户,则更好,您可以使用字典,其中键是 AccountClassificaiton 类型(值为 Account 对象)按类型对链接帐户进行分组:

public Dictionnary<AccountClassificaiton, Account> LinkedAccounts { get; set; }

Of course you can indent the dictionary to get better searching efficiency in case you have many linked accounts:当然,如果您有多个关联帐户,您可以缩进字典以获得更好的搜索效率:

public Dictionnary<AccountClassificaiton, Dictionnary<Guid, Account>> LinkedAccounts { get; set; }

Or use a custom equality comparer treating Guid and AccountClassificaiton kind of a composite key: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iequalitycomparer-1?view=net-5.0或者使用自定义相等比较器处理 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM