c# - 如何在 C# .Net 核心的工厂设计模式中使用反射

标签 c# .net design-patterns reflection .net-core

我已经通过以下方式在 c# .Net 核心中实现了工厂方法。我有几个具体的产品,比如 Gateway1Gateway2

public interface IGateway
{
    void Test();
}

ConcreteCreator:
public class PaymentFactory
{
    private readonly IPaymentTransactionRepository _paymentTransactionRepository;
    private readonly IPaymentGatewayRepository _paymentGatewayRepository;

    public PaymentFactory(IPaymentTransactionRepository paymentTransactionRepository,
        IPaymentGatewayRepository paymentGatewayRepository)
    {
        _paymentTransactionRepository = paymentTransactionRepository;
        _paymentGatewayRepository = paymentGatewayRepository;
    }

    public IGateway ExecuteCreation(string bank)
    {
        switch (bank)
        {
            case "Gateway1":
                {
                    return new Gateway1(_paymentGatewayRepository);
                }
            case "Gateway2":
                {
                    return new Gateway2(_paymentTransactionRepository, _paymentGatewayRepository);

                }
            default:
                {
                    return null;

                }
        }
    }

}

混凝土制品:
public class Gateway1 : IGateway
{
    private readonly IPaymentTransactionRepository _paymentTransactionRepository;

    public Gateway1(IPaymentTransactionRepository paymentGatewayRepository)
    {
        _paymentGatewayRepository = paymentGatewayRepository;
    }

    public void Test()
    {
        //code
    }
}

public class Gateway2 : IGateway
{
    private readonly IPaymentTransactionRepository _paymentTransactionRepository;
    private readonly IPaymentGatewayRepository _paymentGatewayRepository;

    public Gateway2(IPaymentTransactionRepository paymentGatewayRepository,
        IPaymentGatewayRepository paymentGatewayRepository)
    {
        _paymentGatewayRepository = paymentGatewayRepository;
        _paymentGatewayRepository = paymentGatewayRepository;
    }

    public void Test()
    {
        //code
    }
}

此代码正在运行,但我想对其进行两项更改。

1- 如何通过反射实现工厂方法?

2- 如何传入多个参数来创建ConcreteProducts ?

提前致谢。

最佳答案

您可以使用波纹管代码。您需要更改PaymentFactory如下。
您可以使用 IServiceProvider 将服务注入(inject)到使用它的类的构造函数中。

混凝土产品名称:

public enum PaymentGatewayEnum
{
    Gateway1 = 1,
    Gateway2 = 2,
}

然后支付工厂:
public class PaymentFactory
{
    private readonly IServiceProvider _serviceProvider;
    public PaymentFactory(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public IGateway ExecuteCreation(PaymentGatewayEnum bank)
    {
        var services = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes())
            .Where(x => typeof(IGateway).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract)
            .FirstOrDefault(x => string.Equals(x.Name, bank.ToString(), StringComparison.CurrentCultureIgnoreCase));

        return Activator.CreateInstance(services, _serviceProvider) as IGateway;
    }

}

进而:
public class Gateway1 : IGateway
{
    private readonly IPaymentTransactionRepository _paymentTransactionRepository;

    public Gateway1(IServiceProvider serviceProvider)
    {
        _paymentTransactionRepository = (IPaymentTransactionRepository)serviceProvider.GetService(typeof(IPaymentTransactionRepository));
    }

    public void Test()
    {
        //code
    }
}

public class Gateway2 : IGateway
{
    private readonly IPaymentTransactionRepository _paymentTransactionRepository;
    private readonly IPaymentGatewayRepository _paymentGatewayRepository;

    public Gateway2(IServiceProvider serviceProvider)
    {
        _paymentTransactionRepository = (IPaymentTransactionRepository)serviceProvider.GetService(typeof(IPaymentTransactionRepository));
        _paymentGatewayRepository = (IPaymentGatewayRepository)serviceProvider.GetService(typeof(IPaymentGatewayRepository));
    }

    public void Test()
    {
        //code
    }
}

关于c# - 如何在 C# .Net 核心的工厂设计模式中使用反射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59588972/

相关文章:

c# - 析构函数和 C# 规范

c# - ASP.NET jQuery Ajax 调用代码隐藏方法

c# - 巴克莱加密 asp.net (VB to c#)

android - 在 Android 中的 Activity 之间传递数据的更好方法

java - Java 栈和递归

ios - 使用实用程序连接到互联网的设计模式

c# - 如何在 C# 中终止 HttpWebRequest 连接?即使设置超时或读写超时也不起作用

c# - 获取字符串的最后 3 个字符

c# - 在没有新约束的情况下创建 T 的新实例

C# 如何打开文件进行写入并允许其他人读取它