5

Chúng tôi muốn đạt được API dựa trên phiên bản bằng cách sử dụng thương lượng nội dung trong tiêu đề chấp nhận.Phiên bản hợp đồng API Web ASP.NET

Chúng tôi có thể đạt được cho bộ điều khiển & các phương pháp API với một số kế thừa và mở rộng bộ chọn HTTP mặc định.

điều khiển thừa kế được thực hiện sử dụng mã mẫu sau,

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; } 
} 

Chúng tôi đã tạo ở trên kiến ​​trúc để làm mã ít hơn với sự thay đổi trong phiên bản, nói rằng nếu phiên bản 1 có 10 phương pháp API và có sự thay đổi trong một API phương pháp hơn là nó có sẵn trong mã phiên bản 2 mà không sửa đổi 9 khác (chúng được kế thừa từ phiên bản 1).

Bây giờ, vấn đề chính chúng ta đang đối mặt là trong phiên bản hợp đồng vì chúng tôi không thể khởi tạo một thể hiện của một sinh viên trừu tượng. Khi ai đó đang đăng JSON lên phiên bản API phiên bản 1 của StudentV1 phải được chuyển vào phương thức và giống nhau trong phiên bản 2.

Có cách nào để đạt được điều này không?

Cảm ơn trước !!

+3

Trông như thế này: http://stackoverflow.com/questions/21306305/binding-abstract-action-parameters-in-webapi –

+0

Cảm ơn @DanielStackenland !! Chúng tôi không có bất kỳ trường phổ biến nào như productType để xác định JSON đã đăng. Ngoài ra, chúng tôi sẽ có khoảng 50 - 70 lớp học như sinh viên trong API sẽ được phiên bản sau này khi được yêu cầu. –

+0

Mục đích của AbstractStudentController là gì? Tại sao bạn không để cho StudentV1Controller (và V2) kế thừa AbstractBaseController và sử dụng StudentV1 (và V2) làm tham số? –

Trả lời

0

Dựa trên mã được dán của bạn, bạn có thể tạo AbstractStudentController chung. Vì các API mà bạn khai báo trừu tượng phải được triển khai trong mọi phiên bản API và bạn có thể xác định loại có chung. Tôi hy vọng tôi không thiếu một cái gì đó từ mô tả của bạn, bởi vì Patch là thiếu từ việc thực hiện của bạn trong StudentV2Controller, nhưng được tuyên bố trừu tượng. Bạn có muốn lấy được StudentV2Controller từ StudentV1Controller không?

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 
    } 
} 
Các vấn đề liên quan