https://github.com/manne/obviously
Roslyn code generator for semantic types
https://github.com/manne/obviously
code-generator immutable microsoft-aspnetcore-mvc roslyn semantic-types source-generation system-text-json
Last synced: 5 months ago
JSON representation
Roslyn code generator for semantic types
- Host: GitHub
- URL: https://github.com/manne/obviously
- Owner: manne
- License: mit
- Created: 2019-12-30T18:48:49.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-07-09T21:17:23.000Z (almost 2 years ago)
- Last Synced: 2025-08-01T05:59:17.041Z (11 months ago)
- Topics: code-generator, immutable, microsoft-aspnetcore-mvc, roslyn, semantic-types, source-generation, system-text-json
- Language: C#
- Homepage:
- Size: 157 KB
- Stars: 7
- Watchers: 2
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: Readme.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Obviously
## Semantic Types
[](https://www.nuget.org/packages/Obviously.SemanticTypes/) [](https://github.com/manne/obviously/blob/master/LICENSE)
> Create semantic types in seconds
### Installation
Install this NuGet package [Obviously.SemanticTypes](https://www.nuget.org/packages/Obviously.SemanticTypes) (currently in preview).
### Usage
#### Basic
The functionality can be easily used.
Declare a partial class and add the attribute `SemanticType`.
The only parameter of this attribute is the actual type of the semantic type. Here it is `string`.
```CSharp
[SemanticType(typeof(string))]
public partial class EmailAddress { }
```
> ⚠ The class must have the `partial` modifier
> ℹ The class can be `sealed`
##### What's getting generated
This generator creates
* The public constructor with a single parameter of the actual type
* The implementations of
* the `comparable` and `equatable` pattern
* `explicit operator` for the actual type.
> ℹ This and the others packages are compile-time dependencies. So the compiled assembly does __not__ contain any references on one of the NuGet packages. Even the `SemanticType` attribute is __not__ in the compiled assembly
###### Code Generation Example
For details what is getting generated, see [Docs/code_generation.md](Docs/code_generation.md)
#### Advanced
##### Validation
The input value of the constructor can be validated.
Therefore a static method named `IsValid` has to be implemented.
This method must only have a single parameter of the actual type and must have the return type `bool`.
If the value is __not__ valid, an instance of the semantic type cannot be created.
###### Validation Example
The example shows the validation of an email address.
```CSharp
[SemanticType(typeof(string))]
public partial class EmailAddress
{
public static bool IsValid(string value)
{
return value.Contains('@');
}
}
```
## Contribution
* Create a fork and make a Pull Request
* Submit a bug
* Submit an idea
## Credits
* For the inspiration [github.com/mperdeck/semantictypes](https://github.com/mperdeck/semantictypes)
* For the code generator [github.com/AArnott/CodeGeneration.Roslyn](https://github.com/AArnott/CodeGeneration.Roslyn)
* For the examples [github.com/andrewlock/StronglyTypedId](https://github.com/andrewlock/StronglyTypedId)
* For making the creation of the generated code so easy [github.com/KirillOsenkov/RoslynQuoter](https://github.com/KirillOsenkov/RoslynQuoter)
## License
This project is licensed under the MIT License - see the [MIT](License) file for details