gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:36:26 24 4
gpt4 key购买 nike

我已经通过以下方式在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com