Skip to content

Compile-time decorators for .NET Dependency Injection using interceptors — enabling fast, reflection-free service registration without assembly scanning.

License

Notifications You must be signed in to change notification settings

LayeredCraft/decoweaver

DecoWeaver

Compile-time decorator registration for .NET dependency injection

NuGet Build Status License: MIT Documentation


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

📚 View Full Documentation

Installation

Install the NuGet package:

dotnet add package DecoWeaver --prerelease

Ensure your project uses C# 11 or later:

<PropertyGroup>
  <LangVersion>11</LangVersion>
  <!-- or <LangVersion>latest</LangVersion> -->
</PropertyGroup>

Quick Start

1. Mark your implementation with the decorator to apply

using DecoWeaver.Attributes;

[DecoratedBy<LoggingRepository>]
public class UserRepository : IUserRepository
{
    // Your implementation
}

2. Register your service normally

services.AddScoped<IUserRepository, UserRepository>();

3. That's it!

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.

Key Features

  • 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.

Requirements

  • 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.

Documentation

📖 Full Documentation - Comprehensive guides and API reference

Key sections:

Contributing

Contributions are welcome! See our Contributing Guide for details.

License

This project is licensed under the MIT License - see the LICENSE file for details.


Made with ⚡ by the LayeredCraft team

About

Compile-time decorators for .NET Dependency Injection using interceptors — enabling fast, reflection-free service registration without assembly scanning.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages