https://github.com/beardgame/fluentsourcegen
Allows using C# source generators using a fluent, type safe interface rather than string templating.
https://github.com/beardgame/fluentsourcegen
c-sharp c-sharp-10 code-generation csharp csharp-sourcegenerator dotnet dotnet-standard fluent hacktoberfest nuget roslyn-generator source-generation
Last synced: 4 months ago
JSON representation
Allows using C# source generators using a fluent, type safe interface rather than string templating.
- Host: GitHub
- URL: https://github.com/beardgame/fluentsourcegen
- Owner: beardgame
- License: mit
- Created: 2022-04-10T19:41:21.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-05-12T18:15:09.000Z (about 1 year ago)
- Last Synced: 2025-05-12T19:30:37.484Z (about 1 year ago)
- Topics: c-sharp, c-sharp-10, code-generation, csharp, csharp-sourcegenerator, dotnet, dotnet-standard, fluent, hacktoberfest, nuget, roslyn-generator, source-generation
- Language: C#
- Homepage:
- Size: 145 KB
- Stars: 24
- Watchers: 3
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Bearded.FluentSourceGen
[](https://github.com/beardgame/fluentsourcegen/actions/workflows/dotnet-build.yml)
Bearded.FluentSourceGen is a library that can be used to write source generation code for C# using a type safe, fluent interface. This prevents authors of source generators having to use string manupulation to output their source. Using this fluent interface brings several advantages over using string manipulation:
* The interface is type safe, which catched bugs in the code you will generate before it is even generated!
* Refactoring the generated code would require rewriting a lot of strings from scratch, whereas with fluent source generation, it's (almost) as easy as refactoring code you wrote yourself!
* The library can handle several details for you like variable or class names that are not part of the public interface.
* The generated code from this library automatically applies good code practices (sealed classes, readonly structs), unless you specifically give it a reason to override those.
Because an example says more than 1000 words, consider the following code snippet:
```csharp
NewClassBuilder("MyClass")
.AddField("myInt", out var intFieldReference)
.AddField("myString", out var stringFieldReference)
.AddField("derivedBool", out var boolFieldReference)
.AddConstructor(ctor => ctor
.InjectFields(intFieldReference, stringFieldReference)
.AddParameter("myDouble", out _)
.AssignField(boolFieldReference, Expression.FromString("myDouble > 0.5")))
.ToSourceString();
```
The output of this snippet would look something like:
```csharp
sealed class MyClass
{
private readonly int myInt;
private readonly string myString;
private readonly bool derivedBool;
public MyClass(int myInt, string myString, double myDouble)
{
this.myInt = myInt;
this.myString = myString;
this.derivedBool = myDouble > 0.5;
}
}
```
## State of the library
This library is currently under **heavy development**. Many parts of this library do not have all functionality yet, and we expect public interfaces to change over time as we converge on a final workable MVP. PRs in this state are welcome, but it is recommended you follow the contributing guidelines below to help us move in the overall direction we are hoping to go in.
## Contributing
See .
## Releases
This project releases regularly following the versioning system as outlined in the [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard. Prerelease versions are numbered using the suffix `-dev.#`.