Skip to content

Commit

Permalink
models: align with the semver versioning strategy and introduce model…
Browse files Browse the repository at this point in the history
… wrappers

since the models are tied to the version of the api, api versioning itself will be dynamic and based on the model version

the original model will be serialized with the json serializer to its utf8 bytes representation of the model. the model wrapper has the model version as a readonly property based on a constant "currentversion"

with this approach to versioning, there's no such thing as controller duplication to deal with specific versions of the api and models at all. automapper will be tweaked to map between the models and its entity equivalent by adding or removing properties and setting defaults

all the responsibility will be placed on automapper and its mapper profiles to deal with versioning using the model version in the model wrapper

extension methods will also be created to map to and from the serialized model at the level of the api and also pass in a generic type that will be used to determine which model to deserialize to before automapper. example being BrandModel or TyreModel for the tyres model wrapper

when i came across api versioning, everyone duplicates the controllers and i won't tolerate that because it's too much of technical debt. this is the best approach to this problem that i could come up with and i can safely say that this is my very own solution to this problem

also, initially i was thinking of adding the version to the api routes but with this, it introduces another problem. ocelot would have to be modified which is pain. so to avoid that, a header will be sent with the request using the version from the models-library constant "currentversion" which nicely ties in with the version of the api to use. any clients would not need to change any code in order to work with the apis
  • Loading branch information
ShaylenReddy42 committed Nov 4, 2022
1 parent 4c43efc commit 12f290f
Show file tree
Hide file tree
Showing 25 changed files with 76 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global using SeelansTyres.Models.AddressModels.V1;
global using SeelansTyres.Models.IdentityModels.V1;
global using SeelansTyres.Models.OrderModels.V1;
global using SeelansTyres.Models.TyresModels.V1;
global using SeelansTyres.Models.AddressModels.V1_0_0;
global using SeelansTyres.Models.IdentityModels.V1_0_0;
global using SeelansTyres.Models.OrderModels.V1_0_0;
global using SeelansTyres.Models.TyresModels.V1_0_0;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model SeelansTyres.Models.OrderModels.V1.OrderModel
@model SeelansTyres.Models.OrderModels.V1_0_0.OrderModel

<h1 style="color: rgb(255, 40, 0); text-align: center; font-size: 36">Seelan's Tyres</h1>
<p style="text-align: center; font-size: 24">Thank you for shopping with us</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
@using System.Net.Http.Headers
@using System.Reflection

@using SeelansTyres.Models.AddressModels.V1
@using SeelansTyres.Models.IdentityModels.V1
@using SeelansTyres.Models.OrderModels.V1
@using SeelansTyres.Models.TyresModels.V1
@using SeelansTyres.Models.AddressModels.V1_0_0
@using SeelansTyres.Models.IdentityModels.V1_0_0
@using SeelansTyres.Models.OrderModels.V1_0_0
@using SeelansTyres.Models.TyresModels.V1_0_0

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SeelansTyres.Models.AddressModels;

public class AddressModelWrapper
{
public byte[] SerializedAddressModel { get; set; } = Array.Empty<byte>();
public static string ModelVersion => Constants.CurrentVersion;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SeelansTyres.Models.AddressModels;

public static class Constants
{
public static string CurrentVersion => "v1.0.0";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace SeelansTyres.Models.AddressModels.V1;
namespace SeelansTyres.Models.AddressModels.V1_0_0;

public class AddressModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global using SeelansTyres.Models.AddressModels.V1;
global using SeelansTyres.Models.AddressModels.V1_0_0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SeelansTyres.Models.IdentityModels;

public static class Constants
{
public static string CurrentVersion => "v1.0.0";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SeelansTyres.Models.IdentityModels;

public class IdentityModelWrapper
{
public byte[] SerializedIdentityModel { get; set; } = Array.Empty<byte>();
public static string ModelVersion => Constants.CurrentVersion;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace SeelansTyres.Models.IdentityModels.V1;
namespace SeelansTyres.Models.IdentityModels.V1_0_0;

public class CustomerModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace SeelansTyres.Models.IdentityModels.V1;
namespace SeelansTyres.Models.IdentityModels.V1_0_0;

public class PasswordModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace SeelansTyres.Models.IdentityModels.V1;
namespace SeelansTyres.Models.IdentityModels.V1_0_0;

public class RegisterModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace SeelansTyres.Models.IdentityModels.V1;
namespace SeelansTyres.Models.IdentityModels.V1_0_0;

public class UpdateAccountModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global using SeelansTyres.Models.IdentityModels.V1;
global using SeelansTyres.Models.IdentityModels.V1_0_0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SeelansTyres.Models.OrderModels;

public static class Constants
{
public static string CurrentVersion => "v1.0.0";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SeelansTyres.Models.OrderModels;

public class OrderModelWrapper
{
public byte[] SerializedOrderModel { get; set; } = Array.Empty<byte>();
public static string ModelVersion => Constants.CurrentVersion;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace SeelansTyres.Models.OrderModels.V1;
namespace SeelansTyres.Models.OrderModels.V1_0_0;

public class OrderItemModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace SeelansTyres.Models.OrderModels.V1;
namespace SeelansTyres.Models.OrderModels.V1_0_0;

public class OrderModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global using SeelansTyres.Models.OrderModels.V1;
global using SeelansTyres.Models.OrderModels.V1_0_0;
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
global using SeelansTyres.Models.IdentityModels.V1;
global using SeelansTyres.Models.TyresModels.V1;
global using SeelansTyres.Models.IdentityModels.V1_0_0;
global using SeelansTyres.Models.TyresModels.V1_0_0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SeelansTyres.Models.TyresModels;

public static class Constants
{
public static string CurrentVersion => "v1.0.0";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SeelansTyres.Models.TyresModels;

public class TyresModelWrapper
{
public byte[] SerializedTyresModel { get; set; } = Array.Empty<byte>();
public static string ModelVersion => Constants.CurrentVersion;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace SeelansTyres.Models.TyresModels.V1;
namespace SeelansTyres.Models.TyresModels.V1_0_0;

public class BrandModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace SeelansTyres.Models.TyresModels.V1;
namespace SeelansTyres.Models.TyresModels.V1_0_0;

public class TyreModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global using SeelansTyres.Models.TyresModels.V1;
global using SeelansTyres.Models.TyresModels.V1_0_0;

0 comments on commit 12f290f

Please sign in to comment.