Home > Net >  Versioning in C# ASP.NET Core
Versioning in C# ASP.NET Core

Time:03-19

I'm new to versioning in ASP.NET Core 3.1-latest

Suppose my product was shipped with a model as shown here in version 1.0:

public class MySelf
{
   public string Name {get; set;}
  
   public string CarName {get; set;}
    //more properties follows
}

With this model in production, now I want to get list of car names.

So I want to introduce a new property into an existing model class, without breaking version 1.0 or giving clients option that there is new property in v1

public class MySelf
{
   public string Name {get; set;}
  
   public List<string> CarNames {get; set;} // new property
    //more properties follows up to for example 8-10
}

How to do this then version for new model?

Controller example

[ApiVersion("1.0")] ///existing version
[ApiVersion("1.1")] //New Version
public class SelfController : ControllerBase 
{

  [HttPost]
  public IActionResult GetDetails(MySelf myselftDetails)
  {
    //implementation
  }

  [HttPost,MapToVersion("1.1")]
  public IActionResult GetDetails(MySelf myselftDetails)
  {
    //for Version2 how do we solve that
  }
}

CodePudding user response:

One technique we use is to spin up multiple models per version.

For example:

[ApiVersion("1.0")] ///existing version
[ApiVersion("2.0")] //New Version
public class SelfController : ControllerBase 
{

  [HttPost]
  public IActionResult GetDetails(MySelfV1 myselftDetails)
  {
  }

  [HttPost,MapToVersion("2.0")]
  public IActionResult GetDetails(MySelfV2 myselftDetails)
  {
  }
}

Assuming every version is a breaking change without backwards compatibility. Semantic versioning must be a jump from v1 to v2.

  • Related