Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/beakona/AutoInterface
C# interface-to-member source generator
https://github.com/beakona/AutoInterface
csharp csharp-core csharp-sourcegenerator scriban source-generators sourcegenerator templating
Last synced: 3 months ago
JSON representation
C# interface-to-member source generator
- Host: GitHub
- URL: https://github.com/beakona/AutoInterface
- Owner: beakona
- License: mit
- Created: 2020-11-03T08:52:06.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-05-12T23:48:48.000Z (6 months ago)
- Last Synced: 2024-06-21T06:34:59.808Z (5 months ago)
- Topics: csharp, csharp-core, csharp-sourcegenerator, scriban, source-generators, sourcegenerator, templating
- Language: C#
- Homepage:
- Size: 251 KB
- Stars: 73
- Watchers: 2
- Forks: 9
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - AutoInterface
- csharp-source-generators - AutoInterface - ![stars](https://img.shields.io/github/stars/beakona/AutoInterface?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/beakona/AutoInterface?style=flat-square&cacheSeconds=86400) interface-to-member source generator. (Source Generators / Patterns)
README
# AutoInterface
C# [Source Generator](https://github.com/dotnet/roslyn/blob/master/docs/features/source-generators.md) which redirects all interface-calls to one or more backing members. Source code can be generated for a `class`, `record`, or `struct`. It can be generated automatically or by a custom template ([scriban](https://github.com/scriban/scriban), liquid).
Manually written source:
```csharp
interface IPrintable
{
void Print();
}public partial class Person : IPrintable
{
[BeaKona.AutoInterface]
private readonly IPrintable aspect1 = new PersonPrinterV1();
}
```Auto-generated accompanying source:
```csharp
partial class Person
{
void IPrintable.Print() => this.aspect1.Print();
}
```## Ad-hoc adapter pattern
In this example `PersonPrinterV1` does not implement `IPrintable` but does have all members that are required by that interface.
Manually written source:
```csharp
interface IPrintable
{
void Print();
}class PersonPrinterV1
{
void Print() { ... }
}public partial class Person
{
[BeaKona.AutoInterface(typeof(IPrintable))]
private readonly PersonPrinterV1 aspect1 = new PersonPrinterV1();
}
```Auto-generated accompanying source:
```csharp
partial class Person : IPrintable
{
void IPrintable.Print() => this.aspect1.Print();
}
```## Generate code from a template
Manually written source:
```csharp
interface IPrintable
{
void Print();
}public partial class Person : IPrintable
{
// add file mytemplate.scriban in your VS project
// and set it's build action to: 'C# analyzer additional file'
[BeaKona.AutoInterface(TemplateFileName = "mytemplate.scriban")]
private readonly IPrintable? aspect1 = null;
}
```Auto-generated accompanying source:
```csharp
partial class Person
{
..generated from file..
}
```## Partial template
Partial template can be defined inline (from string) or from file.
Manually written source:
```csharp
interface IPrintable
{
int Length { get; }
int Count { get; }
void Print1();
void Print2();
}public partial class Person : IPrintable
{
private void LogDebug(string name) { }[BeaKona.AutoInterface]
[BeaKona.AutoInterfaceTemplate(BeaKona.AutoInterfaceTargets.PropertyGetter,
Filter = "Length", Language = "scriban", Body = "return 1;")]
[BeaKona.AutoInterfaceTemplate(BeaKona.AutoInterfaceTargets.Method,
Filter = "Print(\\d)?", Body="LogDebug(nameof({{interface}}.{{name}})); {{expression}};")]
private readonly IPrintable? aspect1 = new PrinterV1();
}
```Auto-generated accompanying source:
```csharp
partial class Person
{
int IPrintable.Length
{
return 1;
}
int IPrintable.Count => this.aspect1!.Count;void IPrintable.Print1()
{
LogDebug(nameof(IPrintable.Print1));
this.aspect1!.Print1();
}void IPrintable.Print2()
{
LogDebug(nameof(IPrintable.Print2));
this.aspect1!.Print2();
}
}
```
Other examples can be found in [wiki](https://github.com/beakona/AutoInterface/wiki/Examples).
---![.NET Core](https://github.com/beakona/AutoInterface/workflows/.NET%20Core/badge.svg)
[![NuGet](https://img.shields.io/nuget/v/BeaKona.AutoInterfaceGenerator)](https://www.nuget.org/packages/BeaKona.AutoInterfaceGenerator)