Oop 您觉得C语言中敏捷原则、模式和实践的数据访问部分(使用SQL Server)怎么样#
您如何看待这样的数据访问代码:Oop 您觉得C语言中敏捷原则、模式和实践的数据访问部分(使用SQL Server)怎么样#,oop,agile,design-patterns,data-access-layer,open-closed-principle,Oop,Agile,Design Patterns,Data Access Layer,Open Closed Principle,您如何看待这样的数据访问代码: public void AddCusotmer(Cusotmer customer) { //save customer into database ... // save payment type SavePaymentType(customer); //save other data ... } private void SavePaymentType(Customer customer) { if(custom
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
...
}
...
}
就我个人而言,我对这样的代码感觉不太好(使用“是”来检测类型以决定做什么),但作者Robert Martin说这没关系,因为它只在DAL中,所以稍微违反OCP是可以接受的
你觉得怎么样?像这样的代码对我来说味道不好。
你可能正在做你自己的O/R-M,所以不知道所有细节 但是使用接口可能会有所帮助(在这种情况下,支付实体将是带有DAL代码的polutet)。。。味道不好 因此,注册课程可能会起到作用:
private void SavePaymentType(PaymentType )
{
if (paymentType == null)
throw new NotSupportedException("Handle nulls too");
IClassPersister persister;
if (!paymentType2Persister.TryGetValue(paymentType.GetType(), out persister))
throw new ORMException(string.Format("Cannot find persister for {0}", paymentType.GetType().Name))
persister.Save(paymentType);
}
在应用程序启动期间,您可以注册PaymentTypes:
paymentType2Persister.Add(typeof(XXXPayment), new XXXPaymentPersistor);
paymentType2Persister.Add(typeof(YYYPayment), new YYYPaymentPersistor);
// etc
因此,当您需要添加另一种支付类型时,您必须为其实现persistor并进行注册。对我来说,这看起来比原始代码好得多
干杯。是的,我对你的解决方案也感觉好多了。