将带有大量重载的遗留类干净地包装在asp.net webapi后面
本文关键字:包装 asp net 后面 webapi 重载 遗留类 | 更新日期: 2023-09-27 18:21:09
我有一个需要通过WebAPI公开的类。然而,该类有大约十个相同方法的重载,从四个参数到大约12个!我无法更改这个底层类,因为由于其他原因,更改将过于重要,所以我正在尽我所能巧妙地包装这种行为。
我不想在客户端(调用方)和服务器(webAPI)上都编写10个方法来1:1匹配这些方法签名,但我想不出更优雅的方法了。为了更好地解释我的问题,我有一个类:
Class A{
Public string something(int a, int b, int c, int d){}
Public string something(int a, int b, int c, int d, int e){}
Public string something(int a, int b, int c, int d, int e, int f){}
Through to…
Public string something(int a, int b, int c, int d, int e, int f, int g, int h, int I, int j, int k, int l){}
}
显然,它们并不都是int,也不是那么干净,因为这个遗留类中有各种参数组合。
我不想在新的客户端包装器中编写一个GET方法来调用我的API,也不想在API内部编写一个GET方法来接收每个参数,但我想不出更干净或更优雅的方法来实现这一点
理想情况下,我想将某种params对象/字典传递给调用方,然后将其传递给web API,但不知道webapi是否会在HTTP GET中支持这一点?
欢迎就如何最好地解决这一问题提出任何建议或反馈。
这可能会有所帮助。
// Create an EmployeeDTO that will be used as the model,
//i.e. parameter of your controller action
public class EmployeeDTO
{
public int a { get; set; }
public int b { get; set; }
public int c { get; set; }
}
// Create a factory that does the actual work
// of calling one of your Employee constructor
public class EmployeeFactory
{
public Employee GetEmployee(EmployeeDTO eDTO)
{
//based on what values you recieve in the model i.e. EmployeeDTO,
//call the appropriate Employee Constructor
}
}
// Your legacy class
public class Employee
{
public Employee(int a) { }
public Employee(int a, int b) { }
public Employee(int a, int b, int c) {}
//.. and so on
}
您可以使用nuget包webapiproxy
、webapiproxy.csharp
为客户端提供代理js/class