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: 7 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 (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-05-12T23:48:48.000Z (over 1 year ago)
- Last Synced: 2024-08-01T22:43:49.298Z (about 1 year ago)
- Topics: csharp, csharp-core, csharp-sourcegenerator, scriban, source-generators, sourcegenerator, templating
- Language: C#
- Homepage:
- Size: 251 KB
- Stars: 73
- Watchers: 2
- Forks: 10
- Open Issues: 2
- 
            Metadata Files:
            - Readme: README.md
- License: LICENSE
 
Awesome Lists containing this project
- csharp-source-generators - AutoInterface -   interface-to-member source generator. (Source Generators / Patterns)
- RSCG_Examples - AutoInterface
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).
---

[](https://www.nuget.org/packages/BeaKona.AutoInterfaceGenerator)