Home

Awesome

AutoCompose

Nuget Nuget Preview License Downloads GitHub Sponsors GitHub

AutoCompose is a .NET source generator that allows the implementation of an interface via composition without having to implement pass-thru code. There is no runtime reflection.

This allows object composition to be used as easily as inheritance.

Getting Started

Given an interface named ISample:

Create a class implementing ISample via composition using AutoCompose by:

  1. Implementing a partial class
  2. Adding the instance of the interface as a member variable (e.g. _sample)
  3. Injecting the variable in the constructor
  4. Decorating the class with the AutoCompose attribute
[AutoCompose(typeof(ISample), "_sample")]
public partial class Sample: ISample
{
    protected ISample _sample;

    // all non-implemented properties and methods are automatically generated as pass-thru logic
    public Sample(ISample sample) 
    {
        _sample = sample;
    }

    :
}

AutoCompose runs at compile time, and generates the source required to fully implement the interface.

Composition as easily as Inheritance

When trying to decide between inheritance or composition, the amount of pass-thru code that must be coded and maintained often plays a role in the decision.

Example: When we need to override the implementation of a single method for an interface that contains 10 methods, the code burden of implementing and maintaing the 9 pass-thru methods often causes developers to choose inheritance over composition, regardless of other considerations.

With AutoCompose, this issue goes away. The developer is free to choose inheritance or composition for purely architectural reasons, without having to worry about implementation or maintenance difficulties.

When should I use AutoCompose?

Any time you want to use composition, and have at least one property or method that would be implemented as pass-thru code, AutoCompose can take that burden away from you.

Known Limitations

  1. AutoCompose currently works for interfaces only. It DOES NOT work (yet) for abstract classes.

  2. AutoCompose may not work if your code has multiple definitions of an interface with the same name. AutoCompose (currently) just picks the first matching symbol, and so does not discriminate the target-interface based purely on namespaces.

  3. If your code contains extremely deep namespaces and would generate source code files with very long names, then there can be filename-length conflicts with the windows file system.

  4. If your interface contains references to classes or structures in other nuget packages, there can be problems generating, and insufficient error messages are given.