Compile-time decorator registration for .NET dependency injection
DecoWeaver is a .NET incremental source generator that brings compile-time decorator registration to your dependency injection setup. Using C# 11+ interceptors, it automatically wraps your service implementations with decorators at build time—eliminating runtime reflection, assembly scanning, and manual factory wiring.
Why DecoWeaver?
- ⚡ Zero runtime overhead - No reflection or assembly scanning at startup
- 🎯 Type-safe - Catches configuration errors at compile time
- 🚀 Fast - Incremental generation with Roslyn
- 🔧 Simple - Class-level or assembly-level decorator attributes
- 🌐 Flexible - Apply decorators globally or per-implementation
- 📦 Clean - Generates readable, debuggable interceptor code
Install the NuGet package:
dotnet add package DecoWeaver --prereleaseEnsure your project uses C# 11 or later:
<PropertyGroup>
<LangVersion>11</LangVersion>
<!-- or <LangVersion>latest</LangVersion> -->
</PropertyGroup>using DecoWeaver.Attributes;
[DecoratedBy<LoggingRepository>]
public class UserRepository : IUserRepository
{
// Your implementation
}services.AddScoped<IUserRepository, UserRepository>();At compile time, DecoWeaver automatically generates interceptor code that wraps UserRepository with LoggingRepository. When you resolve IUserRepository, you'll get the decorated instance.
LoggingRepository
└─ UserRepository
For more examples including open generics, multiple decorators, and ordering, see the Quick Start Guide.
- Assembly-Level Decorators: Apply decorators to all implementations from one place with
[assembly: DecorateService(...)] - Class-Level Decorators: Apply decorators to specific implementations with
[DecoratedBy<T>] - Keyed Service Support: Works with keyed service registrations like
AddKeyedScoped<T, Impl>(serviceKey) - Factory Delegate Support: Works with factory registrations like
AddScoped<T, Impl>(sp => new Impl(...)) - Instance Registration Support: Works with singleton instances like
AddSingleton<T>(instance) - Opt-Out Support: Exclude specific decorators with
[DoNotDecorate] - Multiple Decorators: Stack multiple decorators with explicit ordering
- Generic Type Decoration: Decorate generic types like
IRepository<T>with open generic decorators - Type-Safe: Compile-time validation catches errors early
- Zero Configuration: No runtime registration or setup needed
- Debuggable: Generated code is readable and inspectable
Learn more in the Core Concepts documentation.
- C# 11+ (for interceptors support)
- .NET 8.0+ (for keyed services)
- .NET SDK 8.0.400+ (Visual Studio 2022 17.11+)
See the Requirements page for full details.
📖 Full Documentation - Comprehensive guides and API reference
Key sections:
- Installation - Get started with DecoWeaver
- Quick Start - Your first decorator in 5 minutes
- Core Concepts - Understand how it works
- Usage Guide - Detailed usage patterns
- Examples - Real-world scenarios
Contributions are welcome! See our Contributing Guide for details.
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ⚡ by the LayeredCraft team