Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Versioning in C# ASP.NET Core

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading