基类静态信息包含不同继承类的信息。如何解决?

时间:2018-08-17 19:48:23

标签: c# oop

我将配置信息存储在基类中以处理继承的类: 我有一个ControlledMainClassDataInColumns基类,该基类处理所有与基于SQL的相关信息,DataRow存储的信息。对于其工作,请使用静态List变量中的配置信息:specialFieldReferences。这由其继承的类填充。

public class ControlledMainClassDataInColumns : NotifyPropertyChangedBase
{
    public static List<TableFieldReference> tableFieldReferences = new List<TableFieldReference>();

    public Open(Int64 ID)
    {
    //...
    }

    public Save();
    {
    //...
    }
}

我已经继承了不同的类的处理方式: 具有以下字段的Employee表:EmployeeID,FirstName,SecondName,EmployeeDeleted,EmployeeValidFrom,... 带有字段的Item teble:ItemID,ItemName,ProductCode,ItemDeleted,ItemValidFrom,... 在首次创建实例期间,配置在Employee和Item类中进行:

Class Employee : ControlledMainClassDataInColumns
{
    //...
        public Employee(Int64 employeeID, DateTime validFrom)
        {
            //If the base class static list is not filled, this is filling
            if (specialFieldReferences.Count == 0)
            {
                specialFieldReferences.Add(new FieldReference(fieldType.ClassControllerField, "EmployeeID"));
                specialFieldReferences.Add(new FieldReference(fieldType.ValidityFromField, "EmployeeValidFrom"));
                specialFieldReferences.Add(new FieldReference(fieldType.DeletedField, "EmployeeDeleted"));
            }

            Open(employeeID);    //In the base class, this handles the loading of data
        }
    //...
}

或在Item类中:

Class Item : ControlledMainClassDataInColumns
{
    //...
        public Item(Int64 itemID, DateTime validFrom)
        {
            //If the base class static list is not filled, this is filling
            if (specialFieldReferences.Count == 0)
            {
                specialFieldReferences.Add(new FieldReference(fieldType.ClassControllerField, "ItemID");
                specialFieldReferences.Add(new FieldReference(fieldType.ValidityFromField, "ItemValidFrom");
                specialFieldReferences.Add(new FieldReference(fieldType.DeletedField, "ItemDeleted");
            }

            Open(itemID);    //In the base class, this handles the loading of data
        }
    //...
}

当我只使用一种类的分配时没有问题:

Employee emp1 = new Employee(1, DateTime.Parse("2018-08-17"));  //First time of Employee, so this fills up specialFieldReferences
//... add some field data
emp1.Save();
// . . .
Employee emp99 = new Employee(99, DateTime.Parse("2018-08-17"));    //99th time, nothing happens about specialFieldReferences
//... add some field data
emp99.Save();

但是当我开始使用Item类时:

Item item1 = new Item(3, DateTime.Parse("2018-08-17"));         //First time of Item, but specialFieldReferences already filled !

我遇到麻烦了。在调试过程中,我看到specialFieldReferences.Count已经是3了!它仍然保留在Employees类的初始化中。 如何处理Employee继承类的静态变量与Item继承类显示的信息不同?

编辑-解决方案
所以我做了什么: 更改了这一行:

public static List<TableFieldReference> tableFieldReferences = new List<TableFieldReference>();

这些行:

public string classIdentifier = null;

private static Dictionary<string, List<FieldReference>> _specialFieldReferences = new Dictionary<string, List<FieldReference>>();

protected List<FieldReference> specialFieldReferences
{
    get
    {
        if (_specialFieldReferences.ContainsKey(classIdentifier))
            return _specialFieldReferences[classIdentifier];
        else
            return null;
    }
    set { _specialFieldReferences[classIdentifier] = value; }
}

并更改了实例的开头:

    public Employee(Int64 employeeID, DateTime validFrom)
    {
        //If the base class static list is not filled, this is filling
        if (specialFieldReferences.Count == 0)

收件人:

    public Employee(Int64 employeeID, DateTime validFrom)
    {
        classIdentifier = "Employee";

        //If the base class static list is not filled, this is filling
        if (specialFieldReferences == null)

现在情况很好!

1 个答案:

答案 0 :(得分:2)

如果必须具有要从中添加/读取数据的静态成员,则在这种情况下,唯一的方法是添加某种区分符。使用此数据的代码将必须考虑到这一点,您可以使用重写的实例字段作为描述符值。在下面的示例中,我使用了硬编码的字符串,但我认为您已经明白了。

我的另一项建议是,如果您要为每种类型的静态变量添加一次数据,请使用静态构造函数,而不是实例构造函数。

public abstract class ControlledMainClassDataInColumns
{
    public static Dictionary<string, List<TableFieldReference>> tableFieldReferences = new Dictionary<string, List<TableFieldReference>>();
}

public class Employee : ControlledMainClassDataInColumns
{
    static Employee()
    {
        var list = new List<TableFieldReference>();
        // populate list
        tableFieldReferences["Employee"] = list;
    }
}