很抱歉,如果标题不能很好地解释这个问题,但我想不出更好的名字。这可能最终成为一个很长的问题,但请耐心等待。
假设我有两种类型的工具Car
和Yacht
,它们扩展了一个名为IVehicle
的界面。
对于这个问题,接口本身并不重要,但是类包含描述它们的属性。
然后我有ITaxCalculator<T> where T : IVehicle
,其中包含方法double Calculate(T vehicle);
。
然后有几个类实现ITaxCalculator<T>
的不同版本(并采用不同的构造函数参数),例如:
LegitYachtTaxCalculator : ITaxCalculator<Yacht>
TaxHavenYachtTaxCalculator : ITaxCalculator<Yacht>
CarTaxCalculator : ITaxCalculator<Car>
然后我有一个List<IVehicle>
包含我的多辆汽车和游艇,我想要计算我必须支付的总税额,同时能够关掉它们用于计算每种税种的方法。
以下是一些代码:
ITaxCalculator<T>
public interface ITaxCalculator<T> where T : IVehicle
{
double Calculate(T vehicle);
}
IVehicle
public interface IVehicle
{
string RegistrationPlate { get; }
}
Car
public class Car : IVehicle
{
public Car(string registrationPlate)
{
RegistrationPlate = registrationPlate;
}
public string RegistrationPlate { get; }
}
Yacht
public class Yacht : IVehicle
{
public int WeightInTons { get; }
public Yacht(string registrationPlate, int weightInTons)
{
RegistrationPlate = registrationPlate;
WeightInTons = weightInTons;
}
public string RegistrationPlate { get; }
}
CarTaxCalculator : ITaxCalculator<Car>
public class CarTaxCalculator : ITaxCalculator<Car>
{
public double Calculate(Car vehicle)
{
Console.WriteLine($"Calculating tax for {vehicle.GetType().FullName} with plate {vehicle.RegistrationPlate} using {this.GetType().FullName}");
return 4999.95;
}
}
LegitYachtTaxCalculator : ITaxCalculator<Yacht>
public class LegitYachtTaxCalculator : ITaxCalculator<Yacht>
{
public double WeightMultiplier { get; }
public LegitYachtTaxCalculator(double weightMultiplier)
{
WeightMultiplier = weightMultiplier;
}
public double Calculate(Yacht vehicle)
{
Console.WriteLine($"Calculating tax for {vehicle.GetType().FullName} with plate {vehicle.RegistrationPlate} using {this.GetType().FullName}");
return vehicle.WeightInTons * WeightMultiplier;
}
}
TaxHavenYachtTaxCalculator : ITaxCalculator<Yacht>
public class TaxHavenYachtTaxCalculator : ITaxCalculator<Yacht>
{
public double Calculate(Yacht vehicle)
{
Console.WriteLine($"Calculating tax for {vehicle.GetType().FullName} with plate {vehicle.RegistrationPlate} using {this.GetType().FullName}");
return 0.0; // No taxes, woho!
}
}
static void Main(string[] args)
{
List<IVehicle> vehicles = new List<IVehicle>
{
new Car("PLT111"),
new Yacht("PLT333", 2500)
};
double totalTaxes = 0;
foreach (var vehicle in vehicles)
{
//Here I want to use engines which are configurable earlier to calculate the taxes for the vehicles
}
}
我试图以多种方式解决这个问题,例如
Dictionary<Type, object>
的类中注册引擎,其中object
是ITaxCalculators注册并通过诸如
public ITaxCalculator<T> GetTaxCalculator<T>() where T : IVehicle
public void RegisterTaxCalculator<T>(ITaxCalculator<T> calculator) where T : IPosition
他们最终都感到过于复杂,使用不良做法或需要太多样板。所以我想知道构建它的最佳方法是什么?我从一开始就走错了轨道,还是有任何我没想过的模式?
谢谢!
答案 0 :(得分:1)
基本上,您需要一种方法来定义税收计算器和车辆之间的映射。
首先,使税务计算器非通用:
public interface ITaxCalculator
{
double Calculate(IVehicle vehicle);
}
更容易发现所有非通用实现,将它们加载到集合中,然后调用Calculate
。
要处理特定的IVehicle
实施细节,请为税务计算器声明基类,并使 it 通用:
public abstract class TaxCalculator<T> : ITaxCalculator
where T : IVehicle
{
public double Calculate(IVehicle vehicle)
{
// this is a single place, where cast is required
return Calculate((T)vehicle);
}
protected abstract double Calculate(T vehicle);
}
映射任务将我们引向元数据 大多数DI容器都具有此功能。例如,这里是Autofac文档。
定义元数据类:
// This is metadata class;
// It defines vehicle type to calculate the tax value
public class TaxCalculatorMetadata
{
public Type VehicleType { get; set; }
}
注册ITaxCalculator
实施:
// Every implementation of ITaxCalculator must be registered like this
builder.RegisterType<CarTaxCalculator>()
.As<ITaxCalculator>()
.WithMetadata<TaxCalculatorMetadata>(m => m.For(_ => _.VehicleType, typeof(Car)));
现在您可以加载所有ITaxCalculator
实现,以某种方式过滤它们(“将它们插入”插槽“”),并使用“插槽”中的车辆类型获取特定计算器:
var vehicles = new List<Vehicle>
{
// ...
};
// assuming, that tax calculators were imported
// using IEnumerable<Lazy<ITaxCalculator, TaxCalculatorMetadata>>
foreach (var vehicle in vehicles)
{
var taxCalculator = taxCalculators
.First(_ => _.Metadata.VehicleType == vehicle.GetType());
Console.WriteLine(taxCalculator.Value.Calculate(vehicle));
}