Monify, a .NET Roslyn source generator, automates the creation of strongly-typed wrappers around single values, improving semantic clarity, type safety, and maintainability.
- C# v2.0 or later.
- Visual Studio 2022 v17.4 or later, or any compatible IDE that supports Roslyn source generators.
To install Monify, use the following command in your package manager console:
install-package MonifyMonify turns a class, record, or struct into a strongly typed wrapper around a single value. To opt in, annotate a partial type with the Monify attribute and specify the encapsulated value type.
using Monify;
[Monify<int>]
public partial struct Age;For language versions earlier than C# 11:
using Monify;
[Monify(Type = typeof(int))]
public partial struct Age;When applied, Monify generates the boilerplate needed for a lightweight value object:
- A private readonly field
_valuethat stores the encapsulated value. - A constructor accepting the underlying value:
Age(int value). - Implicit conversion operators to and from the encapsulated type.
- Implementations of
IEquatable<Age>andIEquatable<int>. - Equality (
==) and inequality (!=) operators for comparing with other instances or with the underlying value. - Overrides of
Equals(object),GetHashCode(), andToString().
Age age = 42; // implicit conversion from int
int value = age; // implicit conversion to int
if (age == value)
{
// comparisons work with both Age and int
}Classes annotated with Monify are sealed automatically to preserve immutability. Types must be partial and cannot declare additional state; the included analyzers will warn when these guidelines are not followed.
Monify includes several analyzers to assist engineers with its usage. These are:
| Rule ID | Category | Severity | Notes |
|---|---|---|---|
| MONFY01 | Usage | Warning | Type is not compatible with Monify |
| MONFY02 | Usage | Warning | Type is not Partial |
| MONFY03 | Design | Error | Type captures State |
| MONFY04 | Design | Error | Self Referencing Type |
Contributions are welcome - see the CONTRIBUTING.md file for details.
This project is licensed under the MIT License - see the LICENSE.md file for details.