Awesome
AutoCompose
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.
- All unimplemented properties and methods on the interface are automatically generated as pass-thru logic.
- Any implemented properties or methods will NOT be generated by AutoCompose.
- As you add or remove implementations, AutoCompose re-generates to compensate.
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:
- Implementing a partial class
- Adding the instance of the interface as a member variable (e.g. _sample)
- Injecting the variable in the constructor
- 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
-
AutoCompose currently works for interfaces only. It DOES NOT work (yet) for abstract classes.
-
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.
-
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.
-
If your interface contains references to classes or structures in other nuget packages, there can be problems generating, and insufficient error messages are given.