5

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와 동일한 방법으로 전달되어야합니다.

이것을 수행 할 수있는 방법이 있습니까?

미리 감사드립니다.

+3

은 다음과 같습니다 http://stackoverflow.com/questions/21306305/binding-abstract-action-parameters-in-webapi –

+0

감사 @DanielStackenland! 게시 된 JSON을 식별하기 위해 productType과 같은 공통 필드가 없습니다. 또한 API에서 student와 같은 약 50 - 70 개의 클래스가 필요하며 나중에 필요할 때 버전이 변경됩니다. –

+0

어쨌든 AbstractStudentController의 목적은 무엇입니까? 왜 StudentV1Controller (및 V2)가 AbstractBaseController를 상속하고 StudentV1 (및 V2)을 매개 변수로 사용하게할까요? –

답변

0

붙여 넣은 코드에 따라 AbstractStudentController 일반을 만들 수 있습니다. 추상을 선언 한 API는 모든 API 버전에서 구현되어야하며 일반을 사용하여 유형을 정의 할 수 있습니다. StudentV2Controller의 구현에서 패치가 누락되었지만 추상으로 선언 되었기 때문에 설명에서 누락 된 것이 없기를 바랍니다. 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 
    } 
}