We would like to achieve version based API using content negotiation in accept header.
我们希望在accept头中使用内容协商来实现基于版本的API。
We are able to achieve for controller & API methods with some inheritance and extending the default HTTP selector.
我们能够通过一些继承实现控制器和API方法,并扩展默认的HTTP选择器。
Controller inheritance is achieved using following sample code,
使用以下示例代码实现控制器继承,
public abstract class AbstractBaseController : ApiController
{
// common methods for all api
}
public abstract class AbstractStudentController : AbstractBaseController
{
// common methods for Student related API'sample
public abstract Post(Student student);
public abstract Patch(Student student);
}
public class StudentV1Controller : AbstractStudentController
{
public override Post([FromBody]Student student) // student should be instance of StudentV1 from JSON
{
// To Do: Insert V1 Student
}
public override Patch([FromBody]Student student) // student should be instance of StudentV1 from JSON
{
// To Do: Patch V1 Student
}
}
public class StudentV2Controller : AbstractStudentController
{
//
public override Post([FromBody]Student student) // student should be instance of StudentV2 from JSON
{
// To Do: Insert V2 Student
}
}
public abstract class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class StudentV1 : Student
{
}
public class StudentV2 : Student
{
public string Email { get; set; }
}
We have created above architecture to do less code with change in version, say if version 1 has 10 API methods and there is a change in one API method than it should be available in version 2 code without modifying other 9(they are inherited from version 1).
我们已经创建了上面的体系结构,以便在版本更改的情况下执行更少的代码,比如说版本1有10个API方法,并且一个API方法的更改比版本2代码中可用的更改而不修改其他9个(它们继承自版本1)。
Now, the main problem we are facing is in contract versioning as we cannot instantiate an instance of an abstract student. When someone is posting JSON to API version 1 instance of StudentV1 should be passed in methods and same in version 2.
现在,我们面临的主要问题是合同版本控制,因为我们无法实例化抽象学生的实例。当有人向API版本1发布JSON时,应该在方法中传递StudentV1的实例,并在版本2中传递相同的实例。
Is there any way to achieve this?
有没有办法实现这个目标?
Thanks in advance!!
提前致谢!!
1 个解决方案
#1
0
Based on your pasted code you could make AbstractStudentController generic. Because those APIs that you declare abstract must be implemented in every API version, and you can define type with the generic. I hope I'm not missing something from your description, because Patch is missing from your implementation in StudentV2Controller, but is declared abstract. Do you wanted to derive StudentV2Controller from StudentV1Controller?
根据您粘贴的代码,您可以使AbstractStudentController成为通用的。因为您声明为abstract的API必须在每个API版本中实现,并且您可以使用泛型定义类型。我希望我没有遗漏你的描述中的内容,因为你的StudentV2Controller实现中缺少Patch,但是它被声明为abstract。你想从StudentV1Controller派生StudentV2Controller吗?
public abstract class AbstractBaseController : ApiController
{
// common methods for all api
}
public abstract class AbstractStudentController<StudentType> : AbstractBaseController
{
// common methods for Student related API'sample
public abstract Post(StudentType student);
public abstract Patch(StudentType student);
}
public class StudentV1Controller : AbstractStudentController<StudentV1>
{
public override Post([FromBody]StudentV1 student) // student should be instance of StudentV1 from JSON
{
// To Do: Insert V1 Student
}
public override Patch([FromBody]StudentV1 student) // student should be instance of StudentV1 from JSON
{
// To Do: Patch V1 Student
}
}
public class StudentV2Controller : AbstractStudentController<StudentV2>
{
//
public override Post([FromBody]StudentV2 student) // student should be instance of StudentV2 from JSON
{
// To Do: Insert V2 Student
}
}
#1
0
Based on your pasted code you could make AbstractStudentController generic. Because those APIs that you declare abstract must be implemented in every API version, and you can define type with the generic. I hope I'm not missing something from your description, because Patch is missing from your implementation in StudentV2Controller, but is declared abstract. Do you wanted to derive StudentV2Controller from StudentV1Controller?
根据您粘贴的代码,您可以使AbstractStudentController成为通用的。因为您声明为abstract的API必须在每个API版本中实现,并且您可以使用泛型定义类型。我希望我没有遗漏你的描述中的内容,因为你的StudentV2Controller实现中缺少Patch,但是它被声明为abstract。你想从StudentV1Controller派生StudentV2Controller吗?
public abstract class AbstractBaseController : ApiController
{
// common methods for all api
}
public abstract class AbstractStudentController<StudentType> : AbstractBaseController
{
// common methods for Student related API'sample
public abstract Post(StudentType student);
public abstract Patch(StudentType student);
}
public class StudentV1Controller : AbstractStudentController<StudentV1>
{
public override Post([FromBody]StudentV1 student) // student should be instance of StudentV1 from JSON
{
// To Do: Insert V1 Student
}
public override Patch([FromBody]StudentV1 student) // student should be instance of StudentV1 from JSON
{
// To Do: Patch V1 Student
}
}
public class StudentV2Controller : AbstractStudentController<StudentV2>
{
//
public override Post([FromBody]StudentV2 student) // student should be instance of StudentV2 from JSON
{
// To Do: Insert V2 Student
}
}