您如何看待这样的数据访问代码:
public void AddCusotmer(Cusotmer customer)
{
//save customer into database
...
// save payment type
SavePaymentType(customer);
//save other data
...
}
private void SavePaymentType(Customer customer)
{
if(customer.PaymentType is XXXPayment)
{
var payment = customer.PaymentType as XXXPayment;
//save payment to XXXPayments table in db
...
}
else if(customer.PaymentType is YYYPayment)
{
var payment = customer.PaymentType as XXXPayment;
//save payment to YYYPayments table in db
...
}
...
}
就个人而言,我对这样的代码感觉不太好(使用“is”来检测类型来决定要做什么),但作者罗伯特马丁说这没关系,因为它只在 DAL 中,所以有点违反 OCP 是可以接受。
你怎么想?