获取对象内的所有关联/复合对象(以抽象方式)

LCJ*_*LCJ 6 c# oop domain-driven-design entity-framework linq-to-sql

业务:

我有一个支付系统,可以通过GiftCoupon,ClubMembershipCard等支付.一个支付本身可以有多个支付组件

课程:

我有一个付款类.它有支付组件,如GiftCouponPayment,ClubMembershipCardPayment,CashPayment等.每种组件类型都满足通用接口IPaymentComponent.我使用有关现有类型的知识实现了它.

问题

1)如何以抽象的方式实现这个功能- 不知道存在哪些类型?这意味着它需要适用于实现IPaymentComponent接口的所有类型.

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 Diagram

在此输入图像描述

参考:

  1. 实体框架4,继承vs扩展?
  2. 如何选择继承策略http://blogs.msdn.com/b/alexj/archive/2009/04/15/tip-12-choosing-an-inheritance-strategy.aspx
  3. 流畅的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;


    }
}
Run Code Online (Sandbox Code Playgroud)

Ada*_*son 1

我想你可能想暂时从设计中退一步。我听到的是这样的:

一笔支付由一个或多个组件组成,每个组件可以是多种类型之一

听起来您需要的是一个Payment表,然后是一个PaymentComponent与该表有外键关系的表PaymentPaymentComponent然后,您可以为您的各种付款方式实施继承。