gpt4 book ai didi

c# - 使实体类因更改而关闭

转载 作者:太空狗 更新时间:2023-10-29 20:10:55 24 4
gpt4 key购买 nike

我有如下所示的数据库关系。域对象是基于 LINQ to SQL ORM 创建的。

支付包括现金支付和礼券支付。假设采购总金额为550,可按以下方式支付

1 Gift Coupon Valued 300

1 Gift Coupon Valued 200

I Cash Currency Valued 50

enter image description here

我正在使用 ORM 的“InsertOnSubmit”功能插入新的付款记录。以下代码工作正常。但是,如果我的公司正在引入使用信用卡的新支付组件,我需要更改我的“支付”域类。我如何使支付类别Open for Extension and Closed for Changes 仍然使用 ORM

注意:Payment 类具有行为(例如 GetTotalAmountCollected)。我正在努力使“支付”类满足 OCP。

注意:优惠券类型有特定的行为。如果优惠券发行日期小于 1/1/2000,则不应将其用于计算总金额(即 CouponValue 应为零)。引用Refactoring code using Strategy Pattern还有。

注意:我使用的是.Net 4.0

引用:

  1. Getting an error when using ObjectContext.AddObject with Entity Framework
  2. Refactoring code using Strategy Pattern
  3. Prefer composition over inheritance?
  4. Code-first vs Model/Database-first
  5. Strategy Pattern and Dependency Injection using Unity
  6. C# Strategy Design Pattern by Delegate vs OOP
  7. How to use the Strategy Pattern with C#?
  8. 首先使用 EF 代码继承:第 2 部分 – 每种类型的表 (TPT) http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-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();
}
}

存储库:

public class LijosPaymentRepository : ILijosPaymentRepository
{
public System.Data.Linq.DataContext MyDataContext { get; set; }

public void InsertEntity(DBML_Project.Payment payment)
{
//Insert the entity
MyDataContext.GetTable<DBML_Project.Payment>().InsertOnSubmit(payment);
}

public void SubmitChanges()
{
MyDataContext.SubmitChanges();
}
}

最佳答案

对于@Lijo 尝试解决抽象方法的问题会更好

我认为您可以在实现您自己的 IPayment 接口(interface)的 CashPayment 类型上创建一个分部类,它可以在整个应用程序中使用。这个接口(interface)也可以在 CreditCardPayment 上:

例子:



public interface IPayment
{
int Id { get; set; }
int PaymentId { get; set; }
//Other payment specific properties or methods
}

public partial class CashPayment : IPayment
{
public int Id
{
get { return CashPaymentId ; }
set { CashPaymentId = value; }
}

//Other properties

}

public partial class CreditCardPayment : IPayment
{
//more code ...
}

在您的 EF 上下文中获取所有付款




public partial class PaymentEntities //The name of your EF entities
{
public IQueryable AllPayments
{
return this.CashPayment.Union(this.CreditCardPayment); //This is not good, but just an example. The abstract class approach would be better here.
}

public void InsertPayment(IPayment payment)
{
this.AddObject(payment.GetType().Name, payment);
}
}

关于c# - 使实体类因更改而关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11425993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com