Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何基于参数从工厂获取实例_C# - Fatal编程技术网

C# 如何基于参数从工厂获取实例

C# 如何基于参数从工厂获取实例,c#,C#,我有一个接口 public interface ITaxService { public DateTime ValidFrom { get; } public DateTime ValidUntil { get; } public decimal GetTax(decimal taxableAmount); } class TaxService2019 : ITaxService { public DateTime ValidFrom { ge

我有一个接口

public interface ITaxService
{
   public DateTime ValidFrom { get; }
   public DateTime ValidUntil { get; }
   public decimal GetTax(decimal taxableAmount);
}
    class TaxService2019 : ITaxService
    {
        public DateTime ValidFrom { get { return new DateTime(2019, 1, 1); } }
        public DateTime ValidUntil { get { return new DateTime(2019, 12, 31); } }

        public decimal GetTax(decimal taxableAmount)
        {
            throw new NotImplementedException();
        }
    }

    class TaxService2020 : ITaxService
    {
        public DateTime ValidFrom { get { return new DateTime(2020, 1, 1); } }
        public DateTime ValidUntil { get { return new DateTime(2020, 12, 31); } }

        public decimal GetTax(decimal taxableAmount)
        {
            throw new NotImplementedException();
        }
    }
我有一些具体的类来实现这个接口

public interface ITaxService
{
   public DateTime ValidFrom { get; }
   public DateTime ValidUntil { get; }
   public decimal GetTax(decimal taxableAmount);
}
    class TaxService2019 : ITaxService
    {
        public DateTime ValidFrom { get { return new DateTime(2019, 1, 1); } }
        public DateTime ValidUntil { get { return new DateTime(2019, 12, 31); } }

        public decimal GetTax(decimal taxableAmount)
        {
            throw new NotImplementedException();
        }
    }

    class TaxService2020 : ITaxService
    {
        public DateTime ValidFrom { get { return new DateTime(2020, 1, 1); } }
        public DateTime ValidUntil { get { return new DateTime(2020, 12, 31); } }

        public decimal GetTax(decimal taxableAmount)
        {
            throw new NotImplementedException();
        }
    }
最后,我有一个工厂,它根据日期为我提供正确的服务

public static class Factory
    {
        static List<Type> TaxTypes;

        static Factory()
        {
            RegisterTypes();
        }

        private static void RegisterTypes()
        {
            TaxTypes = new List<Type>();
            TaxTypes.Add(typeof(TaxService2019));
            TaxTypes.Add(typeof(TaxService2020));
        }
        public ITaxService GetTaxService(DateTime legalDate)
        {
            // What to put here?
        }
公共静态类工厂
{
静态列表分类类型;
静态工厂()
{
RegisterTypes();
}
私有静态无效注册表类型()
{
TaxTypes=新列表();
TaxTypes.Add(typeof(TaxService2019));
TaxTypes.Add(typeof(TaxService2020));
}
公共ITaxService GetTaxService(DateTime legalDate)
{
//在这里放什么?
}
现在,我无法决定如何优雅地获得正确的服务。我尝试了三种方法:

  • 每个日期范围的Else if列表

  • if(新日期时间(2019,1,1)您可以执行以下操作,例如:

    public static class Factory
    {
        static List<(ITaxService service, Func<ITaxService> factory)> TaxDesc;
    
        static Factory()
        {
            RegisterTypes();
        }
    
        private static void RegisterTypes()
        {
            TaxDesc = new List<(ITaxService service, Func<ITaxService> factory)>
            {
                (new TaxService2019(), () => new TaxService2019())
            };
        }
        
        public static ITaxService GetTaxService(DateTime legalDate)
        {
            foreach (var taxDesc in TaxDesc)
            {
                if(taxDesc.service.ValidFrom  <= legalDate && legalDate < taxDesc.service.ValidUntil)
                {
                    return taxDesc.factory();
                }
            }
            
            throw new Exception();
        }
    }
    
    公共静态类工厂
    {
    静态列表TaxDesc;
    静态工厂()
    {
    RegisterTypes();
    }
    私有静态无效注册表类型()
    {
    TaxDesc=新列表
    {
    (新的TaxService2019(),()=>新的TaxService2019())
    };
    }
    公共静态ITaxService GetTaxService(DateTime legalDate)
    {
    foreach(taxDesc中的var taxDesc)
    {
    
    如果(taxDesc.service.ValidFrom),您可以创建实例而不是注册类型吗?