DelegateTo.SourceGenerator 0.0.3

dotnet add package DelegateTo.SourceGenerator --version 0.0.3                
NuGet\Install-Package DelegateTo.SourceGenerator -Version 0.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DelegateTo.SourceGenerator" Version="0.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DelegateTo.SourceGenerator --version 0.0.3                
#r "nuget: DelegateTo.SourceGenerator, 0.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DelegateTo.SourceGenerator as a Cake Addin
#addin nuget:?package=DelegateTo.SourceGenerator&version=0.0.3

// Install DelegateTo.SourceGenerator as a Cake Tool
#tool nuget:?package=DelegateTo.SourceGenerator&version=0.0.3                

Delegate To

Inheritance is often used to share code, but this can lead to unintended complexity. One way around this is to favor composition over inheritance.

Some languages have a bit of syntactic sugar to make delegating behavior to a sub-entity easier:

  • Ruby/Rails allows you to specify a "delegate" where methods of a given name are forwarded to a composite type

    class Greeter < ActiveRecord::Base
        def hello
            'hello'
        end
    end
    
    class Foo < ActiveRecord::Base
        belongs_to :greeter
        delegate :hello, to: :greeter
    end
    
    Foo.new.hello  # implicitly forward "hello" to the contained "Greeter"
    
  • Go has a concept of promoted fields where methods are forwarded to an anonymous composed type

    struct Student {
        Year int
        Person // anonymous person delegate
    }
    
    struct Person {
        Age int
    }
    
    student := Student { Year: 2010, Person: Person { Age: 18 } }
    student.Age // -> 18
    

C# does not have this feature, so inheritance is the easiest way to share code or interfaces between classes.

until now

With source generators, we can forward, at compile time, all desired functionality from parent class to a composed class, and inherit all required interfaces.

Example:

partial class Student
{
    public int Year { get; set; }

    [GenerateDelegate] // signal source generator to create delegate functions
    Person Person { get; set; }
}

class Person
{
    public int Age { get; set; }
}

var student = new Student { Year = 2010, Person = new() { Age = 18 }};
student.Age // -> 18

This will be done by generating the following partial class for student with the needed functionality

partial class Student
{
    public Age { get => Person.Age; set => Person.Age = value; }
}

As a bonus, the Student or parent class should be able to automatically gain any and all interfaces implemented by the composed class as it will now fulfill all of those contracts.

See ./DelegateTo.Test for examples.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.3 437 7/23/2022
0.0.2 387 7/23/2022
0.0.1 389 7/23/2022