Skip to content

ankitvijay/Enumeration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

94 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status NuGet NuGet vijayankit

Nuget Packages

Package Link
AV.Enumeration NuGet
AV.Enumeration.ModelBinder NuGet
AV.Enumeration.SystemTextJson NuGet
AV.Enumeration.NewtonsoftJson NuGet
AV.Enumeration.NSwag NuGet

Enumeration class

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:

  • AV.Enumeration - The Enumeration class package.
  • AV.Enumeration.ModelBinder - Custom ModelBinder to allow Enumeration class pass as a query string parameter.
  • AV.Enumeration.SystemTextJson - System.Text.Json serialization support for Enumeration class.
  • AV.Enumeration.NewtonsoftJson - Newtonsoft.Json serialization support for Enumeration class.
  • AV.Enumeration.NSwag - NSwag support for Enumeration class to generate Enumeration as an Enum type schema.

Want to know more about Enumeration class?

See my Enumeration class blog post series

Give a Star ⭐️

Found this repository helpful? You can give a star. :)

Usage

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

Support

Has my work been helpful to you? You can extend your support 😊

Buy Me A Coffee