{"id":21864789,"url":"https://github.com/fluxera/fluxera.enumeration","last_synced_at":"2025-04-14T21:02:05.032Z","repository":{"id":37855874,"uuid":"435265828","full_name":"fluxera/Fluxera.Enumeration","owner":"fluxera","description":"An object-oriented enumeration library.","archived":false,"fork":false,"pushed_at":"2024-04-13T13:50:46.000Z","size":202,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-05-15T21:07:04.695Z","etag":null,"topics":["ddd","ddd-architecture","ddd-patterns","domain-driven-design","dotnet","dotnet7","enum","enumeration"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fluxera.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-05T20:00:43.000Z","updated_at":"2024-06-02T12:51:18.147Z","dependencies_parsed_at":"2024-03-19T16:30:52.043Z","dependency_job_id":"ffdfe229-0cc2-4ab7-959b-b17f4ed145be","html_url":"https://github.com/fluxera/Fluxera.Enumeration","commit_stats":null,"previous_names":[],"tags_count":48,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fluxera%2FFluxera.Enumeration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fluxera%2FFluxera.Enumeration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fluxera%2FFluxera.Enumeration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fluxera%2FFluxera.Enumeration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fluxera","download_url":"https://codeload.github.com/fluxera/Fluxera.Enumeration/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226857026,"owners_count":17693016,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ddd","ddd-architecture","ddd-patterns","domain-driven-design","dotnet","dotnet7","enum","enumeration"],"created_at":"2024-11-28T04:12:24.728Z","updated_at":"2024-11-28T04:12:29.267Z","avatar_url":"https://github.com/fluxera.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://dev.azure.com/fluxera/Foundation/_apis/build/status/GitHub/fluxera.Fluxera.Enumeration?branchName=main\u0026stageName=BuildAndTest)](https://dev.azure.com/fluxera/Foundation/_build/latest?definitionId=80\u0026branchName=main)\n\n# Fluxera.Enumeration\nAn object-oriented enumeration library.\n\n## Usage\n\nDefine an enumerable as C# class that inherits from the ```Enumeration\u003cTEnum\u003e``` base class.\nAdd the enumeration options as public static readonly fields to the new enumerable class.\n\n```c#\npublic sealed class Color : Enumeration\u003cColor\u003e\n{\n    public static readonly Color Red = new Color(0, \"FF0000\");\n    public static readonly Color Green = new Color(1, \"00FF00\");\n    public static readonly Color Blue = new Color(2, \"0000FF\");\n    public static readonly Color White = new Color(3, \"FFFFFF\");\n    public static readonly Color Black = new Color(4, \"000000\");\n\n    /// \u003cinheritdoc /\u003e\n    private Color(int value, string hexValue, [CallerMemberName] string name = null!) \n        : base(value, name)\n    {\n        this.HexValue = hexValue;\n    }\n\n    public string HexValue { get; }\n}\n```\n\n```c#\npublic class MessageType : Enumeration\u003cMessageType\u003e\n{\n    public static readonly MessageType Email = new EmailType();\n    public static readonly MessageType Postal = new PostalType();\n    public static readonly MessageType TextMessage  = new TextMessageType();\n\n    /// \u003cinheritdoc /\u003e\n    private MessageType(int value, string name) \n        : base(value, name)\n    {\n    }\n\n    private sealed class EmailType : MessageType\n    {\n        /// \u003cinheritdoc /\u003e\n        public EmailType() : base(0, \"Email\")\n        {\n        }\n    }\n\n    private sealed class PostalType : MessageType\n    {\n        /// \u003cinheritdoc /\u003e\n        public PostalType() : base(1, \"Postal\")\n        {\n        }\n    }\n\n    private sealed class TextMessageType : MessageType\n    {\n        /// \u003cinheritdoc /\u003e\n        public TextMessageType() : base(2, \"TextMessage\")\n        {\n        }\n    }\n}\n```\n\n```c#\npublic abstract class Animal : Enumeration\u003cAnimal\u003e\n{\n    /// \u003cinheritdoc /\u003e\n    protected Animal(int value, string name) \n        : base(value, name)\n    {\n    }\n}\n\npublic sealed class Mammal : Animal\n{\n    public static readonly Mammal Tiger = new Mammal(0);\n    public static readonly Mammal Elephant = new Mammal(1);\n\n    /// \u003cinheritdoc /\u003e\n    private Mammal(int value, [CallerMemberName] string name = null!) \n        : base(value, name)\n    {\n    }\n}\n\npublic sealed class Reptile : Animal\n{\n    public static readonly Reptile Iguana = new Reptile(2);\n    public static readonly Reptile Python = new Reptile(3);\n\n    /// \u003cinheritdoc /\u003e\n    private Reptile(int value, [CallerMemberName] string name = null!) \n        : base(value, name)\n    {\n    }\n}\n```\n\nThe default type for the value is ```int```, but several other types can be used as the value: ```byte```, ```short```,```int```,```long```,```decimal```,```float```,```double```,```string``` and ```Guid```.    \n\n```C#\npublic class GuidEnum : Enumeration\u003cGuidEnum, Guid\u003e\n{\n    public static readonly GuidEnum One = new GuidEnum(Guid.Parse(\"7a524c5a-7724-442d-a906-8219bce4a0fd\"), \"One\");\n\n    /// \u003cinheritdoc /\u003e\n    public GuidEnum(Guid value, string name) \n        : base(value, name)\n    {\n    }\n}\n\npublic class LongEnum : Enumeration\u003cLongEnum, long\u003e\n{\n    public static readonly LongEnum One = new LongEnum(999999999999999999, \"One\");\n\n    /// \u003cinheritdoc /\u003e\n    public LongEnum(long value, string name) \n        : base(value, name)\n    {\n    }\n}\n```\n\nThe ```Enumeration\u003cTEnum\u003e``` provides a fluent API to configure switch-case like structures\nto simplify the execution of action for specific cases.\n\n```c#\npublic void PrintColorInfo(Color color)\n{\n    color\n        .When(Color.Red).Then(() =\u003e SetConsoleColor(ConsoleColor.Red))\n        .When(Color.Blue).Then(() =\u003e SetConsoleColor(ConsoleColor.Blue))\n        .When(Color.Green).Then(() =\u003e SetConsoleColor(ConsoleColor.Green))\n        .Default(() =\u003e SetConsoleColor(ConsoleColor.White));\n\n    Console.WriteLine($\"{color}({color.Value}) =\u003e #{color.HexValue}\");\n}\n```\n\n## Serialization Support\n\nAt the moment serialization support is available for:\n\n- [Entity Framework Core](https://github.com/dotnet/efcore)\n- [Newtonsoft.Json (JSON.NET)](https://github.com/JamesNK/Newtonsoft.Json)\n- [LiteDB](https://github.com/mbdavid/LiteDB)\n- [MongoDB](https://github.com/mongodb/mongo-csharp-driver)\n- [System.Text.Json](https://github.com/dotnet/corefx/tree/master/src/System.Text.Json)\n\n### Entity Framework Core\n\nTo support ```Enumeration\u003cTEnum\u003e``` in EFCore use **one** of the available extension methods on the ```ModelBuilder```.\n\n```c#\n// Store the name in the database.\nmodelBuilder.ApplyEnumerationNameConversions();\n\n// Store the value in the database.\nmodelBuilder.ApplyEnumerationValueConversions();\n```\n\n### Newtonsoft.Json (JSON.NET)\n\nTo support ```Enumeration\u003cTEnum\u003e``` in JSON.NET use **one** of the available extensions methods on the ```JsonSerializerSettings```.\n\n```c#\nJsonSerializerSettings settings = new JsonSerializerSettings();\n\n// Use the name to serialize all enumerations.\nsettings.UseEnumerationNameConverter();\n\n// Use the value to serialize all enumerations.\nsettings.UseEnumerationValueConverter();\n\n// Use the name to serialize just the Color enumeration.\nsettings.UseEnumerationNameConverter\u003cColor\u003e();\n\n// Use the value to serialize just the Color enumeration.\nsettings.UseEnumerationValueConverter\u003cColor\u003e();\n\nJsonConvert.DefaultSettings = () =\u003e settings;\n```\n\n### LiteDB\n\nTo support ```Enumeration\u003cTEnum\u003e``` in LiteDB use **one** of the available extensions methods on the ```BsonMapper```.\n\n```c#\n// Store the name in the database for all enumerations available in the given assembly.\nBsonMapper.Global.UseEnumerationName(Assembly.GetExecutingAssembly());\n\n// Store the value in the database for all enumerations available in the given assembly.\nBsonMapper.Global.UseEnumerationValue(Assembly.GetExecutingAssembly());\n\n// Store the name in the database just for the Color enumeration.\nBsonMapper.Global.UseEnumerationName\u003cColor\u003e();\n\n// Store the value in the database just for the Color enumeration.\nBsonMapper.Global.UseEnumerationName\u003cColor\u003e();\n```\n\n### MongoDB\n\nTo support ```Enumeration\u003cTEnum\u003e``` in MongoDB use **one** of the available extensions methods on the ```ConventionPack```.\n\n```c#\nConventionPack pack = new ConventionPack();\n\n// Store the name in the database.\npack.AddEnumerationNameConvention();\n\n// Store the value in the database.\npack.AddEnumerationValueConvention();\n\nConventionRegistry.Register(\"ConventionPack\", pack, t =\u003e true);\n```\n\n### System.Text.Json\n\nTo support ```Enumeration\u003cTEnum\u003e``` in System.Text.Json use **one** of the available extensions methods on the ```JsonSerializerOptions```.\n\n```c#\nJsonSerializerOptions options = new JsonSerializerOptions();\n\n// Use the name to serialize all enumerations.\noptions.UseEnumerationNameConverter();\n\n// Use the value to serialize all enumerations.\noptions.UseEnumerationValueConverter();\n\n// Use the name to serialize just the Color enumeration.\nsettings.UseEnumerationNameConverter\u003cColor\u003e();\n\n// Use the value to serialize just the Color enumeration.\nsettings.UseEnumerationValueConverter\u003cColor\u003e();\n```\n\n## Future\n\nWe plan to implement support for OData server- and client-side to enable queries on ```Enumeration``` like is was an simple C# ```enum```. \nAn ```Enumeration``` should generate an ```EdmEnumType``` in the metadata for an OData feed.\n\nThe simple way would be to add the enumeraions as complex type, but that feels wrong because\nthe object-oriented enumeration should work like an ```enum``` and the EDM model would not\nreflect the available options.\n\n```c#\nEdmEnumType color = new EdmEnumType(\"Default\", \"Color\");\ncolor.AddMember(new EdmEnumMember(color, \"Red\", new EdmIntegerConstant(0)));\ncolor.AddMember(new EdmEnumMember(color, \"Blue\", new EdmIntegerConstant(1)));\ncolor.AddMember(new EdmEnumMember(color, \"Green\", new EdmIntegerConstant(2)));\nmodel.AddElement(color);\n```\n\nThis cannot be achieved using the model builder because the enum methods are tightly bound to C# ```enum```.\n\n```xml\n\u003cEnumType Name=\"Color\"\u003e\n   \u003cMember Name=\"Red\" Value=\"0\" /\u003e\n   \u003cMember Name=\"Blue\" Value=\"1\" /\u003e\n   \u003cMember Name=\"Green\" Value=\"2\" /\u003e\n\u003c/EnumType\u003e\n```\n\nWe want to support filter queries like so:\n\n```plaintext\nhttps://localhost:5001/odata/Cars?$filter=Color eq 'Blue'\nhttps://localhost:5001/odata/Cars?$filter=Color eq 1\n```\n\nhttps://github.com/OData/WebApi/issues/1186\nhttps://github.com/OData/WebApi/blob/master/src/Microsoft.AspNet.OData.Shared/UnqualifiedCallAndEnumPrefixFreeResolver.cs\n\nWe also plan to support Swagger documentation in ASP.NET Core applications.\n\n## References\n\nThis library was inspired by:\n\n[Søren Pedersen](https://gist.github.com/spewu) - [Enumeration](https://gist.github.com/spewu/5933739)\n\n[Steve Smith](https://github.com/ardalis) - [SmartEnum](https://github.com/ardalis/SmartEnum)\n\n[Kyle Herzog](https://github.com/kyleherzog) - [ExtendableEnums](https://github.com/kyleherzog/ExtendableEnums)\n\n[MakoLab S.A.](https://github.com/MakoLab) - [SpecializedEnum](https://github.com/MakoLab/fractus/blob/master/dotNet/CommunicationProjects/zzTest/SpecializedEnum.cs)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluxera%2Ffluxera.enumeration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffluxera%2Ffluxera.enumeration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluxera%2Ffluxera.enumeration/lists"}