|
1 | 1 | # WrapperValueObject |
2 | 2 |
|
3 | | -A .NET source generator for creating simple value objects without too much boilerplate for `Equals`, `GetHashCode` and operators overloads/implementations. |
| 3 | + |
| 4 | +[](https://www.nuget.org/packages/WrapperValueObject.Generator/) |
4 | 5 |
|
5 | | -## Example |
| 6 | +A .NET source generator for creating |
| 7 | +* Simple value objects wrapping other type(s), without the hassle of manual `Equals`/`GetHashCode` |
| 8 | +* Value objects wrapping math primitives |
| 9 | + * I.e. `[WrapperValueObject(typeof(int))] partial readonly struct MeterLength` - the type is implicitly castable to `int`, and you can create your own math operations |
| 10 | +* Strongly typed ID's |
| 11 | + * Similar to F# `type ProductId = ProductId of Guid`, here it becomes `[WrapperValueObject] partial readonly struct ProductId` |
| 12 | + |
| 13 | +Note that record type feature for structs is planned for C# 10, in which cases some of the |
| 14 | +use cases this library supports will be easier to achieve without this libray. |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +Add to your project file: |
| 19 | + |
| 20 | +```xml |
| 21 | +<PackageReference Include="WrapperValueObject.Generator" Version="0.0.1-alpha04"> |
| 22 | + <PrivateAssets>all</PrivateAssets> |
| 23 | + <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> |
| 24 | +</PackageReference> |
| 25 | +``` |
| 26 | + |
| 27 | +Or install via CLI |
| 28 | + |
| 29 | +```sh |
| 30 | +dotnet add package WrapperValueObject.Generator --version 0.0.1-alpha04 |
| 31 | +``` |
| 32 | + |
| 33 | +## Usage |
6 | 34 |
|
7 | 35 | 1. Use the attribute to specify the underlying type. |
8 | 36 | 2. Declare the struct or class with the `partial` keyword. (and does not support nested types) |
9 | 37 |
|
| 38 | +### Simple example |
| 39 | + |
| 40 | +```csharp |
| 41 | +[WrapperValueObject(typeof(int))] |
| 42 | +public readonly partial struct MeterLength |
| 43 | +{ |
| 44 | + public static implicit operator CentimeterLength(MeterLength meter) => meter.Value * 100; |
| 45 | +} |
| 46 | + |
| 47 | +[WrapperValueObject(typeof(int))] |
| 48 | +public readonly partial struct CentimeterLength |
| 49 | +{ |
| 50 | + public static implicit operator MeterLength(CentimeterLength centiMeter) => centiMeter / 100; |
| 51 | +} |
| 52 | + |
| 53 | +MeterLength meters = 2; |
| 54 | + |
| 55 | +CentimeterLength centiMeters = meters; |
| 56 | + |
| 57 | +Assert.Equal(200, (int)centiMeters); |
| 58 | +``` |
| 59 | + |
| 60 | +### Full example |
| 61 | + |
10 | 62 | ```csharp |
11 | 63 | using System; |
12 | 64 | using System.Diagnostics; |
13 | 65 |
|
14 | 66 | namespace WrapperValueObject.TestConsole |
15 | 67 | { |
16 | | - [WrapperValueObject(typeof(Guid))] |
| 68 | + [WrapperValueObject] // Is Guid by default |
17 | 69 | public readonly partial struct MatchId |
18 | 70 | { |
19 | 71 | public static MatchId New() => Guid.NewGuid(); |
@@ -63,3 +115,13 @@ namespace WrapperValueObject.TestConsole |
63 | 115 | } |
64 | 116 | } |
65 | 117 | ``` |
| 118 | + |
| 119 | +## TODO/under consideration |
| 120 | + |
| 121 | +Further development on this PoC was prompted by this discussion: https://github.com/ironcev/awesome-roslyn/issues/17 |
| 122 | + |
| 123 | +* Replace one generic attribute (WrapperValueObject) with two (or more) that cleary identify the usecase. E.g. StronglyTypedIdAttribute, ImmutableStructAttribute, ... |
| 124 | +* Support everything that StronglyTypedId supports (e.g. optional generation of JSON converters). |
| 125 | +* Bring the documentation to the same level as in the StronglyTypedId project. |
| 126 | +* Write tests. |
| 127 | +* Create Nuget package. |
0 commit comments