商业:

我有一个付款系统,可以通过GiftCoupon,ClubMembershipCard等进行付款。一项付款本身可以有多个付款组成部分

:

我有一个付款类(class)。它具有付款组件,如GiftCouponPayment,ClubMembershipCardPayment,CashPayment等。每种组件类型都满足一个公共(public)接口(interface)IPaymentComponent。我已经使用有关现有类型的知识来实现​​它。

问题

1)如何以抽象的方式实现此功能-不知道存在的所有类型是什么?这意味着它需要为实现IPaymentComponent接口(interface)的所有类型工作。

2)如果无法在LINQ to SQL中实现,则在Entity Framework中可以吗?

3)当 LINQ to SQL 在Payment对象内部生成GiftCouponPayment实体时,它是关联/聚合还是组成?

注意:我使用LINQ to SQL作为ORM。 GiftCouponPayment和Payment是自动生成的类,这些对象由ORM创建。我通过使用部分类为这些类添加了更多功能。

注意:在数据库中,每个PaymentComponent(例如GiftCouponPayment)都有自己的属性(例如CouponValue,CardValue等)。因此,逐级表将不是。我们需要单独的表格。那条线有解决方案吗?

注意:此付款之前,GiftCouponPayment已存在于数据库中。我们需要使用客户提供的GiftCouponPaymentID来标识GiftCouponPayment对象。我们只需要更新此表中的PaymentID列即可。



LINQ to SQL图

引用:

  • Entity Framework 4, inheriting vs extending?
  • 如何选择继承策略http://blogs.msdn.com/b/alexj/archive/2009/04/15/tip-12-choosing-an-inheritance-strategy.aspx
  • Fluent API样本-http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-fluent-api-samples.aspx

  • C#代码
    public interface IPaymentComponent
    {
         int MyID { get; set; }
         int MyValue { get; set; }
         int GetEffectiveValue();
    }
    
    
    public partial class GiftCouponPayment : IPaymentComponent
    {
        public int MyID
        {
            get
            {
                return this.GiftCouponPaymentID;
            }
            set
            {
                this.GiftCouponPaymentID = value;
            }
        }
    
        public int MyValue
        {
            get
            {
                return this.CouponValue;
            }
            set
            {
                this.CouponValue = value;
            }
        }
    
        public int GetEffectiveValue()
        {
            if (this.CouponNumber < 2000)
            {
                return 0;
            }
            return this.CouponValue;
        }
    }
    
    public partial class Payment
    {
        public List<IPaymentComponent> AllPaymentComponents()
        {
            List<IPaymentComponent> allPayComps = new List<IPaymentComponent>();
    
    
            List<GiftCouponPayment> giftCouponPaymentList = new List<GiftCouponPayment>();
            List<CashPayment> cashPaymentList = new List<CashPayment>();
    
            foreach (GiftCouponPayment g in this.GiftCouponPayments)
            {
                giftCouponPaymentList.Add(g);
                allPayComps.Add(g);
            }
    
            foreach (CashPayment c in this.CashPayments)
            {
                cashPaymentList.Add(c);
                allPayComps.Add(c);
            }
    
            return allPayComps;
    
    
        }
    }
    

    最佳答案

    我认为您可能想暂时退出设计。我听到的是这样的:



    听起来您需要的是一个Payment表,然后是一个带有外键关系的PaymentComponent表,该表又回到了Payment表。然后,您可以在PaymentComponent表上实现各种付款方式的继承。

    关于c# - 获取对象内的所有关联/复合对象(以抽象方式),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11470037/

    10-10 21:31