【问题标题】:How to use Reflection in factory design patterns in C# .Net core如何在 C# .Net 核心的工厂设计模式中使用反射
【发布时间】:2020-01-04 07:55:59
【问题描述】:

我已经通过以下方式在 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- CreateConcreteProducts如何传入多个参数?

提前致谢。

【问题讨论】:

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


    【解决方案1】:

    您可以使用下面的代码。您需要更改PaymentFactory,如下所示。 您可以用户 IServiceProvider 将服务注入到使用它的类的构造函数中。

    具体产品名称:

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

    然后 PaymentFactory:

    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      相关资源
      最近更新 更多