我正在尝试为我的一个使用Asp.Net核心的项目创建支付网关抽象,以便客户可以通过创建派生库来集成他们的支付网关。我的应用程序将加载派生库并调用这些方法。
下面是所有支付网关都必须实现的接口。它位于Core库中,我们称它为PaymentGateway.Core.dll.这个库是主应用程序的一部分。
namespace PaymentGateway.Core
{
public interface IPaymentGateway
{
string Name { get; }
// The purpose of this function is to transform the order details into an object,
// as expected by the underlying gateway's apis
object CreatePaymentRequest(Order request);
// The purpose of this function is to transform the payment response object,
// received from the payment gateway api,
// into an application entity that represents this payment.
Payment ProcessPaymentResponse(object response);
}
// Order for which the payment to be collected. This entity is stored in DB
public class Order
{
string UserId { get; set; }
string ProductId { get; set; }
double OrderTotal { get; set; }
}
// A payment attempted for an Order. This entity is stored in DB
public class Payment
{
Order Order { get; set; }
string PaymentGateway { get; set; }
double Amount { get; set; }
PaymentStatus Status { get; set; } // Failed, User Aborted, Success
}
}
下面是一个PayPal集成库的例子,我们称它为PaymentGateway.PayPal.dll.该库引用核心库并实现PaymentGateway接口。
namespace PaymentGateway.PayPal
{
class PayPal : IPaymentGateway
{
public string Name { get => "PayPal"; }
public object CreatePaymentRequest(Order request)
{
:
:
}
public Payment ProcessPaymentResponse(object response)
{
:
:
}
}
}
核心库中执行支付的流程如下所示:
我遇到的问题是应用程序(核心)不理解CreatePaymentRequest()的返回类型,因为它依赖于被调用的网关。类似地,对于ProcessPaymentResponse(),参数类型是特定于网关的,类型将在网关库中定义。
现在,我被迫使用System.Object。这个问题有没有更好的解决方案?
https://stackoverflow.com/questions/56587178
复制相似问题