{"id":13628938,"url":"https://github.com/vicosanz/StronglyTypedUid","last_synced_at":"2025-04-17T04:32:36.824Z","repository":{"id":232411132,"uuid":"784319874","full_name":"vicosanz/StronglyTypedUid","owner":"vicosanz","description":"Implementation of Strongly Typed Ids.","archived":false,"fork":false,"pushed_at":"2024-04-09T20:48:20.000Z","size":20,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-21T00:38:19.506Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/vicosanz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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":"2024-04-09T16:06:53.000Z","updated_at":"2024-08-29T20:41:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"faadba54-94db-4e03-806d-f69f14951235","html_url":"https://github.com/vicosanz/StronglyTypedUid","commit_stats":null,"previous_names":["vicosanz/stronglytypeduid"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicosanz%2FStronglyTypedUid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicosanz%2FStronglyTypedUid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicosanz%2FStronglyTypedUid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicosanz%2FStronglyTypedUid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vicosanz","download_url":"https://codeload.github.com/vicosanz/StronglyTypedUid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249316005,"owners_count":21249873,"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":[],"created_at":"2024-08-01T22:00:59.884Z","updated_at":"2025-04-17T04:32:35.011Z","avatar_url":"https://github.com/vicosanz.png","language":"C#","funding_links":["https://www.paypal.com/paypalme/vicosanzdev?locale.x=es_XC"],"categories":["Content"],"sub_categories":["134. [StronglyTypedUid](https://ignatandrei.github.io/RSCG_Examples/v2/docs/StronglyTypedUid) , in the [PrimitiveObsession](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#primitiveobsession) category"],"readme":"# StronglyTypedUid\nC# Implementation of Strongly Typed Id made easy.\n\nStronglyTypedUid [![NuGet Badge](https://buildstats.info/nuget/StronglyTypedUid)](https://www.nuget.org/packages/StronglyTypedUid/)\n\nStronglyTypedUid.Generator [![NuGet Badge](https://buildstats.info/nuget/StronglyTypedUid.Generator)](https://www.nuget.org/packages/StronglyTypedUid.Generator/)\n\n[![publish to nuget](https://github.com/vicosanz/StronglyTypedUid/actions/workflows/main.yml/badge.svg)](https://github.com/vicosanz/StronglyTypedUid/actions/workflows/main.yml)\n\n\n## Buy me a coffee\nIf you want to reward my effort, :coffee: https://www.paypal.com/paypalme/vicosanzdev?locale.x=es_XC\n\n\nAll strongly typed ids are source generated, you must create a record struct in this ways:\n\nUsing attribute decorating a record struct (default Guid version)\n\n```csharp\n    [StronglyTypedUid] \n    public readonly partial record struct CustomerId { }\n```\n\nIf you want change to Ulid\n\n```csharp\n    [StronglyTypedUid(asUlid:true)] \n    public readonly partial record struct CustomerId { }\n```\n\nCreate additional converters to popular packages like efcore, dapper and newtonsoftjson\n\n```csharp\n    [StronglyTypedUid(asUlid:true, [EnumAdditionalConverters.EFCore, EnumAdditionalConverters.Dapper, EnumAdditionalConverters.NewtonsoftJson])]\n    public readonly partial record struct CustomerId { }\n```\n\nThe generator will create a partial record struct of the same name\n\n```csharp\n// Auto generated code\n[TypeConverter(typeof(CustomerIdTypeConverter))]\n[System.Text.Json.Serialization.JsonConverter(typeof(CustomerIdJsonConverter))]\npublic readonly partial record struct CustomerId(Guid Value) : IStronglyTypedGuid\n{\n    public static CustomerId Empty =\u003e new(Guid.Empty);\n\n    public static CustomerId NewCustomerId() =\u003e new(Guid.NewGuid());\n\n    public static implicit operator CustomerId(Guid value) =\u003e new(value);\n\n    public static explicit operator Guid(CustomerId value) =\u003e value.Value;\n\n    public bool IsEmpty =\u003e Value == Guid.Empty;\n\n    public override string ToString() =\u003e Value.ToString();\n\n    public static CustomerId Parse(string text) =\u003e new CustomerId(Guid.Parse(text));\n\n    public static bool TryParse(string text, out CustomerId result)\n    {\n        try\n        {\n            if (Guid.TryParse(text, out Guid uid))\n            {\n                result = uid;\n                return true;\n            }\n        }\n        catch (Exception)\n        {\n        }\n        result = default;\n        return false;\n    }\n}\n```\n\nYou can add additional logic to your strongly type id.\n\n```csharp\n    [StronglyTypedUid] \n    public readonly partial record struct CustomerId \n    { \n        public override string ToTaggedString() =\u003e $\"CID-{Value}\";\n\n        public static bool TryParseTagged(string text, out CustomerId customer)\n        {\n\t\t    try\n\t\t    {\n                if (Guid.TryParse(text[4..], out Guid result))\n                {\n                    customer = result;\n                    return true;\n                }\n            }\n            catch (Exception)\n\t\t    {\n\t\t    }\n            customer = default;\n            return false;\n        }\n    }\n```\n\nThe new type is decorated with a TypeConverter and a JsonConverter automatically\n\n```csharp\n[TypeConverter(typeof(CustomerIdTypeConverter))]\n[System.Text.Json.Serialization.JsonConverter(typeof(CustomerIdJsonConverter))]\n```\n\nYou can serialize and deserialize without problems\n\n\n```csharp\npublic record Customer(CustomerId Id, string Name);\n\n\nvar newcustomer = new Customer(CustomerId.NewCustomerId(), \"Jhon\");\n\nvar serializeOptions = new JsonSerializerOptions\n{\n    WriteIndented = true\n};\nvar json = JsonSerializer.Serialize(newcustomer, serializeOptions);\n\nvar newcustomer2 = JsonSerializer.Deserialize\u003cCustomer\u003e(json);\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvicosanz%2FStronglyTypedUid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvicosanz%2FStronglyTypedUid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvicosanz%2FStronglyTypedUid/lists"}