获取对象内的所有关联/复合对象(以抽象方式) [英] Get all associate/composite objects inside an object (in Abstract way)

查看:148
本文介绍了获取对象内的所有关联/复合对象(以抽象方式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

业务



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





我有一个付款类。它有付款组件,如GiftCouponPayment,ClubMembershipCardPayment,CashPayment等。每个组件类型都满足通用接口IPaymentComponent。我已经使用关于现有类型的知识来实现​​它。



问题



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



2)如果不可能在LINQ to SQL中实现它,那么实体框架?



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. 如何选择继承策略 http://blogs.msdn。 com / b / alexj / archive / 2009/04/15 / tip-12-selection-an-inheritance-strategy.aspx

  2. 流畅的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;组; }
int MyValue {get;组; }
int GetEffectiveValue();
}


公共部分类GiftCouponPayment:IPaymentComponent
{
public int MyID
{
get
{
返回this.GiftCouponPaymentID;
}
set
{
this.GiftCouponPaymentID = value;
}
}

public int MyValue
{
get
{
return this.CouponValue;
}
set
{
this.CouponValue = value;
}
}

public int GetEffectiveValue()
{
if(this.CouponNumber< 2000)
{
返回0;
}
return this.CouponValue;
}
}

public partial class付款
{
public List< IPaymentComponent> AllPaymentComponents()
{
列表< IPaymentComponent> allPayComps = new List< IPaymentComponent>();


列表< GiftCouponPayment> giftCouponPaymentList = new List< GiftCouponPayment>();
列表< CashPayment> cashPaymentList = new List< CashPayment>();

foreach(GiftCouponPayment g in this.GiftCouponPayments)
{
giftCouponPaymentList.Add(g);
allPayComps.Add(g);
}

foreach(CashCaymentP在此CashPayments)
{
cashPaymentList.Add(c);
allPayComps.Add(c);
}

return allPayComps;


}
}


解决方案

我想你可能想退出设计片刻。我听到的是这样的:


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


您需要的是一个付款表, code> PaymentComponent 表与外键关系回到付款表。然后,您可以在 PaymentComponent 表上实现继承,以获得各种付款方式。


Business:

I have a payment system in which payment can be made though GiftCoupon, ClubMembershipCard etc. One payment itself can have multiple payment components

Class:

I have a Payment class. It has payment components like GiftCouponPayment, ClubMembershipCardPayment, CashPayment and so on. Each component type satisfy a common interface IPaymentComponent. I have implemented it using the knowledge about the existing types.

Questions

1) How to implement this function in a abstract way – without knowing what all are the types that exist? That means it need to work for all types that implement IPaymentComponent interface.

2) If it is not possible to achieve it in LINQ to SQL, is it possible in Entity Framework?

3) Is it association / aggregation or composition when LINQ to SQL generate GiftCouponPayment entities inside Payment object?

Note: I am using LINQ to SQL as ORM. GiftCouponPayment and Payment are autogenerated classes and these objects are created by ORM. I have added more functionality to these classes by using partial classes.

Note: In database each PaymentComponent (E.g. GiftCouponPayment) has its own properties (e.g CouponValue,CardValue etc). Hence Table-Per-Hierarchy will not be good. We need separate tables. Is there a solution in that line?

Note: GiftCouponPayment already exist in the database prior to this payment. We need to identify the GiftCouponPayment object by using GiftCouponPaymentID provided by the customer. We just need to update the PaymentID column in this table.

A leaky abstraction refers to any implemented abstraction, intended to reduce (or hide) complexity, where the underlying details are not completely hidden

LINQ to SQL Diagram

REFERENCE:

  1. Entity Framework 4, inheriting vs extending?
  2. How to choose an Inheritance Strategy http://blogs.msdn.com/b/alexj/archive/2009/04/15/tip-12-choosing-an-inheritance-strategy.aspx
  3. Fluent API Samples - http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-fluent-api-samples.aspx

C# CODE

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;


    }
}

解决方案

I think you might want to step back from the design for a moment. What I've heard is this:

A payment consists of one or more components, and each component can be one of a variety of types

What it sounds like you need is a Payment table, then a PaymentComponent table with a foreign key relation back to the Payment table. You can then implement inheritance on the PaymentComponent table for your various forms of payment.

这篇关于获取对象内的所有关联/复合对象(以抽象方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆