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

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

问题描述

商家

我有一个支付系统中,支付虽然可以GiftCoupon,ClubMembershipCard等进行一次性支付本身可以有多种付款组件

我有一个Payment类。它有支付组件,如GiftCouponPayment,ClubMembershipCardPayment,CashPayment等。每种组件类型满足一个公共接口IPaymentComponent。我已经使用关于现有各类知识付诸实施。

问题

1)如何实现在摘要办法此功能 - 不知道什么都是存在的类型?这意味着它需要为实现IPaymentComponent接口,所有类型的工作。

2)如果它是不可能在LINQ实现它到SQL,是否有可能在实体框架?

3)是否有关联/聚合或组合时的的LINQ to SQL 生成付款对象中GiftCouponPayment实体?

请注意:我使用LINQ to SQL作为ORM。 GiftCouponPayment和支付自动生成的类和ORM创建这些对象。我已经使用部分类增加了更多的功能,这些类。

请注意:在数据库中每个PaymentComponent(例如GiftCouponPayment)都有自己的属性(例如COUPONVALUE,CardValue等)。因此,表格,每层次也不会好。我们需要独立的表。有没有在该行的解决方案?

请注意:GiftCouponPayment已经存在于在此之前支付数据库。我们需要通过使用由客户提供GiftCouponPaymentID识别GiftCouponPayment对象。我们只需要更新此表中的列PaymentID


  

一个漏水的抽象是指任何实施抽象,旨在减少(或隐藏)的复杂性,其中底层细节不完全隐藏


的LINQ to SQL图

参考


  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#code

 公共接口IPaymentComponent
{
     INT MYID {搞定;组; }
     INT myvalue的{搞定;组; }
     INT GetEffectiveValue();
}
公共部分类GiftCouponPayment:IPaymentComponent
{
    公众诠释MYID
    {
        得到
        {
            返回this.GiftCouponPaymentID;
        }
        组
        {
            this.GiftCouponPaymentID =价值;
        }
    }    公众诠释myvalue的
    {
        得到
        {
            返回this.CouponValue;
        }
        组
        {
            this.CouponValue =价值;
        }
    }    公众诠释GetEffectiveValue()
    {
        如果(this.CouponNumber&下; 2000)
        {
            返回0;
        }
        返回this.CouponValue;
    }
}公共部分类付款
{
    公开名单< IPaymentComponent> AllPaymentComponents()
    {
        清单< IPaymentComponent> allPayComps =新的List< IPaymentComponent>();
        清单< GiftCouponPayment> giftCouponPaymentList =新的List< GiftCouponPayment>();
        清单< CashPayment> cashPaymentList =新的List< CashPayment>();        的foreach(在this.GiftCouponPayments GiftCouponPayment G)
        {
            giftCouponPaymentList.Add(G);
            allPayComps.Add(G);
        }        的foreach(在this.CashPayments CashPayment C)
        {
            cashPaymentList.Add(C);
            allPayComps.Add(C);
        }        返回allPayComps;
    }
}


解决方案

我想你可能想一步从设计回顾一下。我听说是这样的:


  

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

1

这听起来像你需要的是一个付款表中,那么 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天全站免登陆