使实体类因更改而关闭
我有一个数据库关系,如下所示。域对象是基于LINQ to SQL ORM创建的。
付款包括现金付款和礼券付款。假设总购买金额为550。可以按以下组成部分付款
1 Gift Coupon Valued 3001 Gift Coupon Valued 200I Cash Currency Valued 50
我正在使用ORM的“ InsertOnSubmit”功能插入新的付款记录。以下代码运行正常。但是,如果公司要使用信用卡引入新的付款方式,则需要更改“付款”域类。我如何仍使用ORM使付款类别对扩展开放,对变更封闭?
注意:Payment类具有行为(例如GetTotalAmountCollected)。我正在尝试使“支付”类满足OCP。
注意:优惠券类型有特定的行为。如果优惠券发行日期小于2000年1月1日,则不应将其用于总金额的计算(即,优惠券值应为零)。另请参阅使用策略模式重构代码。
注意:我正在使用.Net 4.0
参考:
首先使用EF代码进行继承:第2部分–每个类型的表(TPT)http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first -ctp5部分-2-表每类型tpt.aspx
C#代码:
public class PaymentAppService{
public RepositoryLayer.ILijosPaymentRepository Repository { get; set; }
public void MakePayment()
{
DBML_Project.Payment paymentEntity = new DBML_Project.Payment();
paymentEntity.PaymentID = 1;
paymentEntity.PaymentType = "PurchaseP";
DBML_Project.CashPayment cashObj = new DBML_Project.CashPayment();
cashObj.CashPaymentID = 1;
cashObj.CurrencyNumber = 123;
cashObj.CurrencyValue = 100;
DBML_Project.GiftCouponPayment giftCouponObj = new DBML_Project.GiftCouponPayment();
giftCouponObj.GiftCouponPaymentID = 1;
giftCouponObj.CouponValue = 200;
giftCouponObj.CouponNumber = 124;
paymentEntity.CashPayments = new System.Data.Linq.EntitySet<DBML_Project.CashPayment>();
paymentEntity.CashPayments.Add(cashObj);
paymentEntity.GiftCouponPayments = new System.Data.Linq.EntitySet<DBML_Project.GiftCouponPayment>();
paymentEntity.GiftCouponPayments.Add(giftCouponObj);
Repository.InsertEntity(paymentEntity);
Repository.SubmitChanges();
}}慕工程0101907