| Package | Link | | ——— | ———| | AV.Enumeration | | | AV.Enumeration.ModelBinder | | | AV.Enumeration.SystemTextJson | | | AV.Enumeration.NewtonsoftJson | | | AV.Enumeration.NSwag | |
This project implements Enumeration class as an alternate to Enum types. The implementation is inspired from famous eShopOnContainers example.
The project provides following NuGet packages:
ModelBinder
to allow Enumeration class pass as a query string parameter.System.Text.Json
serialization support for Enumeration class.Newtonsoft.Json
serialization support for Enumeration class.See my Enumeration class blog post series
Found this repository helpful? You can give a star. :)
PaymentType
Enumeration class (Import: AV.Enumeration
)public class PaymentType : Enumeration
{
public static readonly PaymentType DebitCard = new PaymentType(0);
public static readonly PaymentType CreditCard = new PaymentType(1);
private PaymentType(int value, [CallerMemberName] string name = null) : base(value, name)
{
}
}
PaymentType
Enumeration class with Behaviour (Import: AV.Enumeration
)public abstract class PaymentType : Enumeration
{
public static readonly PaymentType DebitCard = new DebitCardType();
public static readonly PaymentType CreditCard = new CreditCardType();
public abstract string Code { get; }
private PaymentType(int value, string name = null) : base(value, name)
{
}
private class DebitCardType : PaymentType
{
public DebitCardType() : base(0, "DebitCard")
{
}
public override string Code => "DC";
}
private class CreditCardType : PaymentType
{
public CreditCardType() : base(1, "CreditCard")
{
}
public override string Code => "CC";
}
}
System.Text.Json
Serialization/Deserialization (Import: AV.Enumeration.SystemTextJson
)public class EnumerationJsonConverterTests
{
private readonly ITestOutputHelper _testOutputHelper;
public EnumerationJsonConverterTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
[Fact]
public void EnumerationSerializesAndDeserializesCorrectly()
{
var expected = new Transaction
{
Amount = 100,
PaymentType = PaymentType.CreditCard
};
var json = JsonSerializer.Serialize(expected,
new JsonSerializerOptions
{
Converters =
{
new EnumerationJsonConverter()
}
});
_testOutputHelper.WriteLine(json);
var actual= JsonSerializer.Deserialize<Transaction>(json, new JsonSerializerOptions()
{
Converters = { new EnumerationJsonConverter() }
});
Assert.Equal(expected.Amount, actual.Amount);
Assert.Equal(expected.PaymentType, actual.PaymentType);
}
}
Newtonsoft.Json
Serialization/Deserialization (Import: AV.Enumeration.NewtonsoftJson
)public class EnumerationJsonConverterTests
{
private readonly ITestOutputHelper _testOutputHelper;
public EnumerationJsonConverterTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
[Fact]
public void EnumerationIsSerializesAndDeserializesCorrectly()
{
var expected = new Transaction
{
Amount = 100,
PaymentType = PaymentType.CreditCard
};
var json = JsonConvert.SerializeObject(expected, Formatting.Indented, new EnumerationJsonConverter());
_testOutputHelper.WriteLine(json);
var actual = JsonConvert.DeserializeObject<Transaction>(json, new EnumerationJsonConverter());
Assert.Equal(expected.Amount, actual.Amount);
Assert.Equal(expected.PaymentType, actual.PaymentType);
}
}
PaymentType
Enumeration as a query string parameter (Import: AV.Enumeration.ModelBinder
)// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(options =>
{
options.ModelBinderProviders.Insert(0, new EnumerationQueryStringModelBinderProvider());
});
}
// Controller
[ApiController]
[Route("[controller]")]
public class TransactionController : ControllerBase
{
[HttpGet]
[Route("code")]
public string Get(PaymentType paymentType)
{
return paymentType.Code;
}
}
Has my work been helpful to you? You can extend your support :blush: