A .NET source generator that enables composition without having to implement pass-thru code. No runtime reflection.
$ dotnet add package Farskeptic.AutoComposeAutoCompose is a .NET source generator that enables composition without having to implement pass-thru code. There is no runtime reflection.
This allows composition to be used as easily as inheritance.
Given an interface named ISample:
Create a class implementing ISample via composition using AutoCompose by:
[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.
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 maintaining 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.
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.