我正在制作一个简单的 Web 表单来收集客户数据并将其输入数据库。我有 5 个子类:Customer
、Bank
、Employee
、Owner
,TradeReference
它们继承自抽象类DataEntry
. DataEntry
有一个功能public void InsertSelfIntoDataBase(int id);
。id 参数是 Customers 表中的主键(Bank、Employee、Owner 和 TradeReference 与 Customer 具有多对一关系),因此Customer
不需要插入 id(CustomerID 在数据库中自动递增) )。
目前,我的代码设置为Bank
, Employee
, Owner
, 并在父类中TradeReference
实现InsertSelfIntoDataBase
函数,同时Customer
抛出 NotImplementedException,因此Customer
类代码的代码看起来有点像这样:
public int InsertSelfIntoDataBase()
{
int customerID = InsertCustomerAndReturnScalor();
return customerID;
}
public override void insertSelfIntoDataBase(int id)
{ throw new NotImplementedException("Customer does not use this function"); }
这个实现是有效的,但它让我觉得我必须使用 NotImplementedException;就像我无法摆脱那种感觉,我的大学教授不知何故知道并在默默地评判我。有没有更好的方法来做到这一点?