C# 在asp.net webapi后面清晰地包装一个具有大量重载的遗留类?
我有一个类需要通过WebAPI公开。然而,这个类有大约十个相同方法的重载,从四个参数到大约12个!我无法更改这个基础类,因为由于其他原因,这个更改太重要了,所以我正试图尽可能简洁地包装这个行为 我不想在客户端(调用者)和服务器(webAPI)上都编写10个方法来匹配这些方法签名1:1,但想不出更优雅的方法来实现这一点。 为了更好地解释我的问题,我有一门课:C# 在asp.net webapi后面清晰地包装一个具有大量重载的遗留类?,c#,asp.net-web-api,C#,Asp.net Web Api,我有一个类需要通过WebAPI公开。然而,这个类有大约十个相同方法的重载,从四个参数到大约12个!我无法更改这个基础类,因为由于其他原因,这个更改太重要了,所以我正试图尽可能简洁地包装这个行为 我不想在客户端(调用者)和服务器(webAPI)上都编写10个方法来匹配这些方法签名1:1,但想不出更优雅的方法来实现这一点。 为了更好地解释我的问题,我有一门课: Class A{ Public string something(int a, int b, int c, int d){} Public
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,也不是很干净,因为在这个遗留类中有各种参数组合
我不想在调用API的新客户端包装器中编写GET方法,也不想在API本身中编写GET方法来接收每个参数,但我想不出更干净或更优雅的方法来实现这一点
理想情况下,我希望将某种类型的params对象/字典传递给调用者,然后将其传递给webapi,但不知道webapi是否会在httpget中支持这一点
欢迎就如何最好地解决这一问题提出任何建议或反馈。这可能会有所帮助
// 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,这可能会有所帮助
// 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