Giter VIP home page Giter VIP logo

enumeration's Introduction

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

enumeration's People

Contributors

ankitvijay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

enumeration's Issues

inheritance issue

Hello

below code will raise error. how to fix it ? thank you.
Enumeration.FromName< StatesPaymentType >("DebitCard");
or
Enumeration.FromValue< StatesPaymentType >(0);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.