Accept 헤더에서 콘텐츠 협상을 사용하여 버전 기반 API를 구현하고자합니다.ASP.NET 웹 API 계약 버전 관리
일부 상속을 사용하고 기본 HTTP 선택기를 확장하여 컨트롤러 & API 메소드를 구현할 수 있습니다.
컨트롤러 상속 우리는 버전의 변화에 적은 코드를 할 수있는 아키텍처 위에 만든
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; }
}
는 버전 1 10 개 API 방법이 있으면 말을 한 API의 변화가, 다음 예제 코드를 사용하여 달성 메서드는 다른 버전 9를 수정하지 않고 버전 2 코드에서 사용할 수 있어야합니다 (버전 1에서 상속 됨).
지금 우리가 직면 한 주된 문제는 추상 학생의 인스턴스를 인스턴스화 할 수 없으므로 계약 버전 관리에 있습니다. JSON을 API 버전 1에 게시 할 때 StudentV1의 인스턴스는 메소드 2와 동일한 방법으로 전달되어야합니다.
이것을 수행 할 수있는 방법이 있습니까?
미리 감사드립니다.
은 다음과 같습니다 http://stackoverflow.com/questions/21306305/binding-abstract-action-parameters-in-webapi –
감사 @DanielStackenland! 게시 된 JSON을 식별하기 위해 productType과 같은 공통 필드가 없습니다. 또한 API에서 student와 같은 약 50 - 70 개의 클래스가 필요하며 나중에 필요할 때 버전이 변경됩니다. –
어쨌든 AbstractStudentController의 목적은 무엇입니까? 왜 StudentV1Controller (및 V2)가 AbstractBaseController를 상속하고 StudentV1 (및 V2)을 매개 변수로 사용하게할까요? –