{"id":19784809,"url":"https://github.com/sm-g/byvalue","last_synced_at":"2025-04-30T22:32:34.436Z","repository":{"id":89390830,"uuid":"156066692","full_name":"sm-g/ByValue","owner":"sm-g","description":"DDD ValueObject implementation helper","archived":false,"fork":false,"pushed_at":"2022-02-05T06:27:00.000Z","size":75,"stargazers_count":9,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-27T00:17:24.823Z","etag":null,"topics":["c-sharp","ddd","domain-driven-design","dotnet","netstandard"],"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/sm-g.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}},"created_at":"2018-11-04T09:26:02.000Z","updated_at":"2023-01-10T22:19:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"56c15197-1c4e-4fc4-9c2a-3670b6f577bd","html_url":"https://github.com/sm-g/ByValue","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sm-g%2FByValue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sm-g%2FByValue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sm-g%2FByValue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sm-g%2FByValue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sm-g","download_url":"https://codeload.github.com/sm-g/ByValue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251791898,"owners_count":21644480,"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":["c-sharp","ddd","domain-driven-design","dotnet","netstandard"],"created_at":"2024-11-12T06:12:47.790Z","updated_at":"2025-04-30T22:32:34.089Z","avatar_url":"https://github.com/sm-g.png","language":"C#","readme":"# ByValue\n\n[![Build status](https://ci.appveyor.com/api/projects/status/k6nmr1mdixf7xho6/branch/master?svg=true)](https://ci.appveyor.com/project/sm-g/byvalue/branch/master) [![Build Status](https://travis-ci.org/sm-g/ByValue.svg?branch=master)](https://travis-ci.org/sm-g/ByValue) [![NuGet](http://img.shields.io/nuget/v/ByValue.svg)](https://www.nuget.org/packages/ByValue/)\n\nThis library helps to create ValueObjects with properly implemented equality behavior:\n\n1. Provides base `ValueObject` class.\n2. Gives extension `ByValue()` for comparing collections with semantic of ValueObject (`IReadOnlyCollection`, `IReadOnlyDictionary`, `IDictionary` and `ISet` are supported).\n\n## Example\n\n```cs\npublic class MultilineAddress : ValueObject\n{\n    public MultilineAddress(IReadOnlyCollection\u003cstring\u003e addressLines, string city, string postalCode)\n    {\n        AddressLines = addressLines ?? throw new ArgumentNullException(nameof(addressLines));\n        City = city ?? throw new ArgumentNullException(nameof(city));\n        PostalCode = postalCode ?? throw new ArgumentNullException(nameof(postalCode));\n\n        if (addressLines.Count \u003c 1 || addressLines.Count \u003e 3)\n            throw new ArgumentOutOfRangeException(nameof(addressLines), addressLines, \"Multiline address should have from 1 to 3 address lines\");\n    }\n\n    public IReadOnlyCollection\u003cstring\u003e AddressLines { get; }\n    public string City { get; }\n    public string PostalCode { get; }\n\n    // here you should return values, which will be used in Equals() and GetHashCode()\n    protected override IEnumerable\u003cobject\u003e Reflect()\n    {\n        // by default collections compared with not strcit ordering\n        yield return AddressLines.ByValue(Ordering.Strict);\n\n        // you can transform object's properties when return them\n        yield return City.ToUpperInvariant();\n\n        yield return PostalCode;\n    }\n}\n```\n\n### SingleValueObject\n\nInherit from `SingleValueObject` to boost performance when ValueObject has only one property:\n\n```cs\npublic class UserId : SingleValueObject\u003cint\u003e\n{\n    public UserId(int value)\n        : base(value)\n    {\n        if (value == 0)\n            throw new ArgumentOutOfRangeException(nameof(value));\n    }\n\n    public static explicit operator UserId(int value)\n    {\n        return new UserId(value);\n    }\n\n    public static implicit operator int(UserId userId)\n    {\n        return userId == null ? 0 : userId.Value;\n    }\n\n    public static implicit operator int? (UserId userId)\n    {\n        return userId == null ? (int?)null : userId.Value;\n    }\n}\n```\n\n### Custom EqualityComparer\n\nWhen using `ByValue()` to compare collections, elements will be compared using default `EqualityComparer`. Sometimes this is not acceptable.\n\nHere is `AddressBook` which implements value object semantic:\n\n```cs\npublic class AddressBook : ReadOnlyCollection\u003cMultilineAddress\u003e\n{\n    public AddressBook(IList\u003cMultilineAddress\u003e list)\n        : base(list)\n    {\n    }\n\n    public override bool Equals(object obj)\n    {\n        if (obj is null)\n            return false;\n        if (ReferenceEquals(this, obj))\n            return true;\n        if (obj.GetType() != GetType())\n            return false;\n\n        var other = obj as AddressBook;\n        var thisByValue = this.ByValue(); // not strict ordering\n        var otherByValue = other.ByValue();\n        return thisByValue.Equals(otherByValue);\n    }\n\n    public override int GetHashCode()\n    {\n        return Items.Count;\n    }\n}\n```\n\nBut you need address book which will not treat address with city \"Foo\" equal to address with city \"fOO\". Possible solution is to use custom `EqualityComparer` for `MultilineAddress` in derived `EnhancedAddressBook`:\n\n```cs\npublic class EnhancedAddressBook : AddressBook\n{\n    public EnhancedAddressBook(IList\u003cMultilineAddress\u003e list)\n        : base(list)\n    {\n    }\n\n    public override bool Equals(object obj)\n    {\n        if (obj is null)\n            return false;\n        if (ReferenceEquals(this, obj))\n            return true;\n        if (obj.GetType() != GetType())\n            return false;\n\n        var other = obj as EnhancedAddressBook;\n        var thisByValue = this.ByValue(x =\u003e x.UseComparer(EnhancedAddressComparer.Instance));\n        var otherByValue = other.ByValue(x =\u003e x.UseComparer(EnhancedAddressComparer.Instance));\n        return thisByValue.Equals(otherByValue);\n    }\n\n    public override int GetHashCode()\n    {\n        return base.GetHashCode();\n    }\n\n    private class EnhancedAddressComparer : IEqualityComparer\u003cMultilineAddress\u003e\n    {\n        public static EnhancedAddressComparer Instance =\u003e new EnhancedAddressComparer();\n\n        public bool Equals(MultilineAddress x, MultilineAddress y)\n        {\n            if (ReferenceEquals(x, y))\n                return true;\n            if (ReferenceEquals(x, null) || ReferenceEquals(y, null))\n                return false;\n\n            return x.AddressLines.ByValue(Ordering.Strict).Equals(y.AddressLines.ByValue(Ordering.Strict))\n                // do not ignore case of chars\n                \u0026\u0026 x.City == y.City\n                \u0026\u0026 x.PostalCode == y.PostalCode;\n        }\n\n        public int GetHashCode(MultilineAddress obj)\n        {\n            return 1;\n        }\n    }\n}\n\n```\n\nMore examples could be found in [tests](https://github.com/sm-g/ByValue/tree/master/test/ByValue.Tests/Samples).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsm-g%2Fbyvalue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsm-g%2Fbyvalue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsm-g%2Fbyvalue/lists"}