加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET Web API合同版本控制

发布时间:2020-12-16 07:42:05 所属栏目:asp.Net 来源:网络整理
导读:我们希望在accept头中使用内容协商来实现基于版本的API. 我们能够实现控制器和具有一些继承和扩展默认HTTP选择器的API方法. 使用以下示例代码实现控制器继承, public abstract class AbstractBaseController : ApiController{ // common methods for all api
我们希望在accept头中使用内容协商来实现基于版本的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方法的更改比版本2代码中可用的更改而不修改其他9个(它们是从版本继承的) 1).

现在,我们面临的主要问题是合同版本控制,因为我们无法实例化抽象学生的实例.当有人将JSON发布到API版本1时,应该在方法中传递StudentV1的实例,并在版本2中传递相同的实例.

有没有办法实现这个目标?

提前致谢!!

解决方法

根据您粘贴的代码,您可以使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
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读