{"id":13629572,"url":"https://github.com/HamedFathi/EnumerationClassGenerator","last_synced_at":"2025-04-17T09:34:54.722Z","repository":{"id":65782325,"uuid":"429272334","full_name":"HamedFathi/EnumerationClassGenerator","owner":"HamedFathi","description":"A C# source generator to create an enumeration class from an enum type.","archived":false,"fork":false,"pushed_at":"2022-08-19T17:30:14.000Z","size":25,"stargazers_count":16,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-11-06T05:41:11.475Z","etag":null,"topics":["csharp","csharp-sourcegenerator","csharp9","dotnet","enum","enumeration","sourcegenerator"],"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/HamedFathi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"HamedFathi","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2021-11-18T02:41:07.000Z","updated_at":"2024-09-14T23:21:20.000Z","dependencies_parsed_at":"2023-02-09T19:25:10.543Z","dependency_job_id":null,"html_url":"https://github.com/HamedFathi/EnumerationClassGenerator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HamedFathi%2FEnumerationClassGenerator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HamedFathi%2FEnumerationClassGenerator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HamedFathi%2FEnumerationClassGenerator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HamedFathi%2FEnumerationClassGenerator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HamedFathi","download_url":"https://codeload.github.com/HamedFathi/EnumerationClassGenerator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223751247,"owners_count":17196594,"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":["csharp","csharp-sourcegenerator","csharp9","dotnet","enum","enumeration","sourcegenerator"],"created_at":"2024-08-01T22:01:13.892Z","updated_at":"2024-11-08T20:31:17.704Z","avatar_url":"https://github.com/HamedFathi.png","language":"C#","readme":"![code](https://user-images.githubusercontent.com/8418700/142345474-3ef91fec-380a-42e2-b95f-c6f04a8f58f5.png)\n\n## Problem\n\n\u003e Enumeration or Enum types are an integral part of C# language. They have been around since the inception of language. Unfortunately, the Enum types also have some limitations. Enum types are not object-oriented. When we have to use Enums for control flow statements, behavior around Enums gets scattered across the application. We cannot inherit or extend the Enum types. These issues can especially be a deal-breaker in Domain-Driven Design (DDD).\u003cbr/\u003e Read more [here](https://ankitvijay.net/2020/05/21/introduction-enumeration-class/) and [here](https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types).\n\nRegardless of the behaviors, there is a repetitive section where you have to write your class fields similar to what you have in a `enum` type.\n\n```cs\n// Enum\npublic enum CardType\n{\n    Amex,\n    Visa,\n    MasterCard\n}\n\n// Converts to\n\n// Enumeration Class\npublic class CardType : Enumeration\n{\n    public static CardType Amex = new(1, nameof(Amex));\n    public static CardType Visa = new(2, nameof(Visa));\n    public static CardType MasterCard = new(3, nameof(MasterCard));\n\n    public CardType(int id, string name)\n        : base(id, name)\n    {\n    }\n    \n    // Other behaviors\n}\n\n```\n\n## Solution\n\nThis generator helps you to focus on your business, no need to know about this pattern or details of `Enumeration` base class.\n\nDefine your `enum` type` and put `EnumerationClass` attribute on top of it.\n\n```cs\nnamespace MyLibrary\n{\n    [EnumerationClass]\n    public enum CardType\n    {\n        Amex,\n        Visa,\n        MasterCard\n    }\n}\n```\n\nYour enumeration class is ready and generated! before saying how to extend it, I should say how to find it!\n\nIf you use parameter-less `EnumerationClass` the default options are:\n\n* Same `namespace` as you `enum` =\u003e  `MyLibrary` for this sample.\n* The class name will be combination of `enum` type name + \"Enumeration\" =\u003e `CardTypeEnumeration` for this sample.\n\nBut you can customize them:\n\n```cs\nnamespace MyLibrary\n{\n    [EnumerationClass(\"MyClass\", \"MyNamespace\")]\n    public enum CardType\n    {\n        Amex,\n        Visa,\n        MasterCard\n    }\n}\n```\n\nThe generated class is `partial` to help you add your business rules, so you can find it as following based on above setting:\n\n```cs\n// How to extend it?\n// Use same namespace name as you defined.\n// Use same class name as you defined.\n// Use 'partial' keyword for the class.\n// To access the base class functionality you should inherit from 'Enumeration'.\n\nnamespace MyNamespace\n    public partial class MyClass /*: Enumeration*/\n    {\n        public void MyNewMethod()\n        {\n            // MasterCard\n        }\n        \n        // ...\n    }\n}\n```\n\n## `Enumeration` Base Class\n\nAs you can see there is a base class that provides some functionalities which includes the following:\n\n```cs\n// Returns 'Name' property.\noverride string ToString()\n\n// Returns all fields\nIEnumerable\u003cT\u003e GetAll\u003cT\u003e()\n\n// Returns difference of two enumerations in integer.\nint AbsoluteDifference(Enumeration firstValue, Enumeration secondValue)\n\n// Returns value from an integer number.\nT FromValue\u003cT\u003e(int value)\n\n// Returns value from a string name.\nT FromDisplayName\u003cT\u003e(string displayName)\n\n// Parse a value based on a condition.\nT Parse\u003cT, K\u003e(K value, string description, Func\u003cT, bool\u003e predicate)\n\n// Parse a value based on a condition with an result.\nbool TryParse\u003cT, K\u003e(K value, string description, Func\u003cT, bool\u003e predicate, out T result)\n\n// Returns status of comparison in number.\nint CompareTo(object other)\n```\n\n\u003chr/\u003e\n\n### [Nuget](https://www.nuget.org/packages/EnumerationClassGenerator)\n\n[![Open Source Love](https://badges.frapsoft.com/os/mit/mit.svg?v=102)](https://opensource.org/licenses/MIT)\n![Nuget](https://img.shields.io/nuget/v/EnumerationClassGenerator)\n![Nuget](https://img.shields.io/nuget/dt/EnumerationClassGenerator)\n\n```\nInstall-Package EnumerationClassGenerator\n\ndotnet add package EnumerationClassGenerator\n```\n\n\u003chr/\u003e\n\n![1](https://user-images.githubusercontent.com/8418700/142345352-f5f306f3-a62d-4dc0-a9e4-d2f87dc827a5.png)\n\n![2](https://user-images.githubusercontent.com/8418700/142345361-fb724ee1-5f47-4058-bb77-7af2d16f9fdd.png)\n\n![3](https://user-images.githubusercontent.com/8418700/142345365-136dbf37-fd82-4a60-b03e-ae46104bb3b6.png)\n\n\u003chr/\u003e\n\n\u003cdiv\u003eIcons made by \u003ca href=\"https://www.freepik.com\" title=\"Freepik\"\u003eFreepik\u003c/a\u003e from \u003ca href=\"https://www.flaticon.com/\" title=\"Flaticon\"\u003ewww.flaticon.com\u003c/a\u003e\u003c/div\u003e\n\n\n","funding_links":["https://github.com/sponsors/HamedFathi"],"categories":["Source Generators","Do not want to test 112 ( old ISourceGenerator )"],"sub_categories":["Enums","1. [ThisAssembly](https://ignatandrei.github.io/RSCG_Examples/v2/docs/ThisAssembly) , in the [EnhancementProject](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#enhancementproject) category"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FHamedFathi%2FEnumerationClassGenerator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FHamedFathi%2FEnumerationClassGenerator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FHamedFathi%2FEnumerationClassGenerator/lists"}