简体   繁体   中英

Versioning WebAPI - model changes

I'm building a RESTful API using ASP.NET WebAPI and I need to support different API versions. I know the ways to implement controller selection based on URL (api/v1/ style) or based on custom HTTP header. This way I'm going to have different controllers in different namespaces for different API versions. However I have another issue to think about. What if my models change between v1 and v2? How do I keep my v1 API from changing its serialized output? I'm using Entity Framework Code First approach, so model changes are easy to implement, but this would possibly break my v1 API. What is the best approach to ensure that each API version stays consistent?

I don't see what does Entity Framework has to do with it. I hope you are not serializing directly your EF models, are you? If you do, then you have a problem indeed.

You should use view models instead. Those view models could then be versioned the same way as you versioned your controllers. You will have different view models for the different versions of your contollers.

This way you have total control over what gets serialized over the wire. Now you could change your EF models internally as much as you want. And one day you might even decide to drop EF (coz it sucks and there might be a new kid on the block) and replace it with something entirelt different and this change won't reflect at all over your Web API and what gets exposed to the outside simply because you are exposing view models.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM