Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/RyotaMurohoshi/ValueObjectGenerator
ValueObjectGenerator is Generator for ValueObjects.
https://github.com/RyotaMurohoshi/ValueObjectGenerator
csharp csharp-library csharp-sourcegenerator
Last synced: about 1 month ago
JSON representation
ValueObjectGenerator is Generator for ValueObjects.
- Host: GitHub
- URL: https://github.com/RyotaMurohoshi/ValueObjectGenerator
- Owner: RyotaMurohoshi
- License: mit
- Created: 2020-11-03T15:33:10.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2020-11-30T12:08:05.000Z (about 4 years ago)
- Last Synced: 2024-10-30T17:24:40.321Z (about 1 month ago)
- Topics: csharp, csharp-library, csharp-sourcegenerator
- Language: C#
- Homepage:
- Size: 13.7 KB
- Stars: 40
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
- RSCG_Examples - ValueObjectGenerator
- csharp-source-generators - ValueObjectGenerator - ![stars](https://img.shields.io/github/stars/RyotaMurohoshi/ValueObjectGenerator?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/RyotaMurohoshi/ValueObjectGenerator?style=flat-square&cacheSeconds=86400) C# source generator is for ValueObjects (ie.Wrapper classes). (Source Generators / Functional Programming)
README
# ValueObjectGenerator
`ValueObjectGenerator` is C# source generator is for ValueObjects (ie.Wrapper classes).
This project is under the developπ§.
## Install
Under the develop.
## Usage
```csharp
using ValueObjectGenerator;[IntValueObject]
public partial class ProductId
{
}
```With `IntValueObject` attribute, below code is generated.
```csharp
public partial class ProductId: IEquatable
{
public int Value { get; }public ProductId(int value)
{
Value = value;
}public override bool Equals(object obj) => ReferenceEquals(this, obj) || obj is ProductId other && Equals(other);
public override int GetHashCode() => Value.GetHashCode();
public override string ToString() => Value.ToString();
public static bool operator ==(ProductId left, ProductId right) => Equals(left, right);
public static bool operator !=(ProductId left, ProductId right) => !Equals(left, right);public bool Equals(ProductId other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Value == other.Value;
}public static explicit operator ProductId(int value) => new ProductId(value);
public static explicit operator int(ProductId value) => value.Value;
}
````ProductId` is a `ValueObject` class (ie.Wrapper classes).
Below table shows all supporting Attributes and corresponding Types.
| attribute | type |
----|----
| StringValueObject | string |
| IntValueObject | int |
| LongValueObject | long |
| FloatValueObject | float |
| DoubleValueObject | double |## Motivation
Next `Product` class has 2 int type properties, `ProductId` and `ProductCategoryId`.
Some methods need `ProductId` as an argument, the other methods need `ProductCategoryId` as an argument.
Sometime, developers may mistake between `ProductId` and `ProductCategoryId`.```csharp
public class Product
{
public Product(string name, int productId, int productCategoryId)
{
Name = name;
ProductId = productId;
ProductCategoryId = productCategoryId;
}public string Name { get; }
public int ProductId { get; }
public int ProductCategoryId { get; }
}
```To avoid this mistake, we should create `ProductId` class and `CategoryId` class, and use them.
With these classes, compiler can detect wrong usage of `ProductId` property and `ProductCategoryId` property.```csharp
public sealed class ProductId: IEquatable
{
public int Value { get; }public ProductId(int value)
{
Value = value;
}public override bool Equals(object obj) => ReferenceEquals(this, obj) || obj is ProductId other && Equals(other);
public override int GetHashCode() => Value.GetHashCode();
public override string ToString() => Value.ToString();
public static bool operator ==(ProductId left, ProductId right) => Equals(left, right);
public static bool operator !=(ProductId left, ProductId right) => !Equals(left, right);public bool Equals(ProductId other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Value == other.Value;
}public static explicit operator ProductId(int value) => new ProductId(value);
public static explicit operator int(ProductId value) => value.Value;
}public class CategroyId: IEquatable
{
public int Value { get; }public CategroyId(int value)
{
Value = value;
}public override bool Equals(object obj) => ReferenceEquals(this, obj) || obj is CategroyId other && Equals(other);
public override int GetHashCode() => Value.GetHashCode();
public override string ToString() => Value.ToString();
public static bool operator ==(CategroyId left, CategroyId right) => Equals(left, right);
public static bool operator !=(CategroyId left, CategroyId right) => !Equals(left, right);public bool Equals(CategroyId other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Value == other.Value;
}public static explicit operator CategroyId(int value) => new CategroyId(value);
public static explicit operator int(CategroyId value) => value.Value;
}public class Product
{
public Product(string name, ProductId productId, CategroyId productCategoryId)
{
Name = name;
ProductId = productId;
ProductCategoryId = productCategoryId;
}public string Name { get; }
public ProductId ProductId { get; }
public CategroyId ProductCategoryId { get; }
}
```There are many boilerplate code in `ProductId` and `CategoryId` classes. These boilerplate code is so noisy to read other important meaningful code.
`C# Source Generator` can generate these boilerplate code with smart way. `ValueObjectGenerator` generate ValueObject classes (ie.Wrapper classes) with `C# Source Generator`.
Below code is example of `ValueObjectGenerator`. There are no boilerplate code to create ValueObjects (ie.Wrapper classes ) like `ProductId` and `CategoryId` classes.
```csharp
[StringValueObject]
public class ProductName { }[IntValueObject]
public class ProductId { }[IntValueObject]
public class CategoryId { }public class Product
{
public Product(ProductName name, ProductId productId, CategoryId productCategoryId)
{
Name = name;
ProductId = productId;
ProductCategoryId = productCategoryId;
}public ProductName Name { get; }
public ProductId ProductId { get; }
public CategoryId ProductCategoryId { get; }
}
```## Plans
* IComparable support.
* JSON serializer/deserializer.
* other value types.
* arithmetic operators support.## Author
Ryota Murohoshi is game Programmer in Japan.
* Posts:http://qiita.com/RyotaMurohoshi (JPN)
* Twitter:https://twitter.com/RyotaMurohoshi (JPN)## License
This library is under MIT License.