0

如何声明用于泛型实例的变量?

在控制器中,我需要创建依赖于支付类型的实例,并且每个类都有不同类型的参数。这就是我使用泛型类型的原因。但我不知道我需要设置什么类型来为每个支付类定义变量。

参数模型

public class PaymentModel
{   
    public string orderNo { get; set;}
}

public class CCPaymentModel : PaymentModel
{
    public string CCNo {get; set;}
    public string expDate {get; set;}
}

public class PaypalPaymentModel : PaymentModel
{
    public string paypalID {get; set;}
}

public class GooglePaymentModel : PaymentModel
{
    public string googleID {get; set;}
} 

接口类,我使用通用类型参数,因为每种支付类型需要不同类型的参数。

public interface IPayment<T> where T : PaymentModel
{
    void makePayment(string orderNo);
    void makeRefund(T refundInfo);
}

楷模,

public class SagePayment
    : IPayment<CreditCardPaymentInfo>
{
    public void MakePayment( CreditCardPaymentInfo creditCardPaymentInfo ) {
        // make payment
    }

    public void MakeRefund( CreditCardPaymentInfo creditCardPaymentInfo ) {
        // make refund
    }
}

public class GooglePayment
    : IPayment<GooglePaymentModel>
{
    public void MakePayment( GooglePaymentModel paymentInfo ) {
        // make payment
    }

    public void MakeRefund( GooglePaymentModel paymentInfo ) {
        // make refund
    }
}

public class PaypalPayment
    : IPayment<PayPalPaymentModel>
{
    public void MakePayment( PayPalPaymentModel paymentInfo ) {
        // make payment
    }

    public void MakeRefund( PayPalPaymentModel paymentInfo ) {
        // make refund
    }
}

控制器(创建实例)

public void Charge(string paytype,orderNo){

    IPayment<???> paymentProcess; // //Error    1   Using the generic type 'com.WebUI.Models.IPayment<T>' requires 1 type arguments
    Object payinfo;  //

    if (Regex.IsMatch(paytype, "^Credit Card"))
    {
        paymentProcess = new SagePayment();
        payinfo = getPaymentInfo(paytype, orderNo); // it return CCPaymentModel type object
    }
    else if (Regex.IsMatch(paytype, "^PayPal"))
    {
        paymentProcess = new PayPalPayment();
        payinfo = getPaymentInfo(paytype, orderNo); // it return PaypalPaymentModel type object
    }
    else if (Regex.IsMatch(paytype, "^Google"))
    {
        paymentProcess = new GooglePayment();
        payinfo = getPaymentInfo(paytype, orderNo); // it return GooglePaymentModel type object
    }

    paymentProcess.MakePayment(payinfo);
}

为了避免错误,我可以这样做,

public void Charge(string paytype,orderNo){

    if (Regex.IsMatch(paytype, "^Credit Card"))
    {
        IPayment<CCPaymentModel> paymentProcess = new SagePayment();
        payinfo = getPaymentInfo(paytype, orderNo);
        paymentProcess.MakePayment(payinfo);
    }
    else if (Regex.IsMatch(paytype, "^PayPal"))
    {
        IPayment<PaypalPaymentModel> paymentProcess = new PayPalPayment();
        payinfo = getPaymentInfo(paytype, orderNo);
        paymentProcess.MakePayment(payinfo);
    }
    else if (Regex.IsMatch(paytype, "^Google"))
    {
        IPayment<GooglePaymentModel> paymentProcess = new GooglePayment();
        payinfo = getPaymentInfo(paytype, orderNo);
        paymentProcess.MakePayment(payinfo);
    }
}

public void Refund(string paytype,orderNo){

    IPayment<???> paymentProcess; // //Error    1   Using the generic type 'com.WebUI.Models.IPayment<T>' requires 1 type arguments
    Object payinfo;  //

    if (Regex.IsMatch(paytype, "^Credit Card"))
    {
        paymentProcess = new SagePayment();
        payinfo = getPaymentInfo(paytype, orderNo); // it return CCPaymentModel type object
    }
    else if (Regex.IsMatch(paytype, "^PayPal"))
    {
        paymentProcess = new PayPalPayment();
        payinfo = getPaymentInfo(paytype, orderNo); // it return PaypalPaymentModel type object
    }
    else if (Regex.IsMatch(paytype, "^Google"))
    {
        paymentProcess = new GooglePayment();
        payinfo = getPaymentInfo(paytype, orderNo); // it return GooglePaymentModel type object
    }

    paymentProcess.MakeRefund(payinfo);
}

但我知道这不是正确的方法。

有谁知道,请指教。

4

2 回答 2

3

鉴于您的MakePayment方法不需要,在单独的接口中声明T它可能是最简单的。您可以使通用接口扩展非通用接口:

// Capitalization fixed to comply with conventions
public interface IPayment
{
    void MakePayment(string orderNo);
}

public interface IRefundPayment<T> : IPayment where T : PaymentModel
{
    void MakeRefund(T refundInfo);
}

或者只是将它们作为单独的接口:

public interface IPaymentHandler
{
    void MakePayment(string orderNo);
}

public interface IRefundHandler<T> where T : PaymentModel
{
    void MakeRefund(T refundInfo);
}

无论哪种方式,您都只需要Charge方法中的非通用接口。

于 2012-11-05T19:51:43.343 回答
1

您可以对以下类型Charge()进行getPaymentInfo()通用化PaymentModel

void Charge<TPaymentModel>(...) where TPaymentModel : PaymentModel {
    IPayment<TPaymentModel> payment = GetPayment<TPaymentModel>();
    // ...
    payment.MakePayment(getPaymentInfo<TPaymentModel>(...));
}

IPayment<TPaymentModel> GetPayment<TPaymentModel>() where TPaymentModel : IPaymentModel
{
    // Create payment of appropriate type based on typeof(TPaymentModel)
}

TPaymentModel GetPaymentInfo(...) where TPaymentModel : PaymentModel
{
    // Create payment model of appropriate type based on typeof(TPaymentModel)
}

这仍然有点难看,要解决这个问题,您可以引入一些新类来隐藏接口中类型之间的对应关系,并使设计更加合理:

/// Abstracts over different ways of making payments
interface IPaymentMaker 
{
    void MakePayment(string payType, long orderNo);
    // MakeRefund etc.
}

/// Refactor code common to all payment types here, and handle the association 
/// between payment and payment model.
class PaymentMakerBase<TPaymentModel> : IPaymentMaker 
    where TPaymentModel : IPaymentModel 
{
    void MakePayment(string payType, long orderNo) 
    {
        NewPayment().MakePayment(NewPaymentModel(payType, orderNo));
    }

    abstract IPayment<TPaymentModel> NewPayment();
    abstract TPaymentModel NewPaymentModel(string payType, long orderNo);
}

/// Handle only the differences between payment types that can't be put inside their
/// implementations
class PaypalPaymentMaker : PaymentMakerBase<PaypalPayment> 
{
    IPayment<PaypalPayment> NewPayment() { ... }
    PaypalPayment NewPaymentModel(...) { ... }
}

static class PaymentMakerFactory 
{
    /// The only "not type safe" part, handles parsing the payType string and 
    /// resolving it to the correct `PaymentMaker`
    public IPaymentMaker GetPaymentMaker(string payType) 
    {
        if (Regex.IsMatch(payType, ...)) 
        {
            // return appropriate payment maker for the payType
        }
        else if (...) 
        {
            // ...
        }
    }
}

然后,您的控制器代码仅如下所示:

PaymentMakerFactory.GetPaymentMaker(payType).MakePayment(payType, orderNo);

显然,可以通过删除冗余来改进上述设计(在我包含它的任何地方都可能不需要 payType),使其更“客观”(而不是传递相同的参数列表),或更方便(PaymentMakerFactory可能会更改为外观这会创建正确的付款人,然后MakePayment立即致电)。

于 2012-11-05T20:51:39.920 回答