InterceptAttribute
NexusLabs.Needlr¶
NexusLabs.Needlr¶
InterceptAttribute Class¶
Applies an interceptor to a class or method. When discovered by Needlr's source generator, a proxy class is generated that routes method calls through the specified interceptor(s).
Inheritance System.Object 🡒 System.Attribute 🡒 InterceptAttribute
Example¶
// Class-level interception (all methods)
[Intercept(typeof(LoggingInterceptor))]
[Scoped]
public class OrderService : IOrderService
{
public Task<Order> GetOrderAsync(int id) => ...;
public Task CreateOrderAsync(Order order) => ...;
}
// Method-level interception
[Scoped]
public class ProductService : IProductService
{
[Intercept(typeof(CachingInterceptor))]
public Task<Product> GetProductAsync(int id) => ...;
// This method is NOT intercepted
public Task UpdateProductAsync(Product product) => ...;
}
// Multiple interceptors with ordering
[Intercept(typeof(LoggingInterceptor), Order = 1)]
[Intercept(typeof(TimingInterceptor), Order = 2)]
[Scoped]
public class ReportService : IReportService { }
Remarks¶
This attribute can be applied at the class level (intercepts all methods) or at the method level (intercepts only that method).
The interceptor type must implement IMethodInterceptor and will be resolved from the DI container, allowing it to have dependencies.
When multiple interceptors are applied, use the Order property to control execution order. Lower values execute first (outermost in the chain).
Note: Interceptors are only supported with source generation. They are not available when using reflection-based registration.
Constructors¶
InterceptAttribute(Type) Constructor¶
Initializes a new instance of the InterceptAttribute class.
Parameters¶
interceptorType System.Type
The type of the interceptor. Must implement IMethodInterceptor.
Exceptions¶
System.ArgumentNullException
Thrown when interceptorType is null.
Properties¶
InterceptAttribute.InterceptorType Property¶
Gets the type of the interceptor to apply.
Property Value¶
InterceptAttribute.Order Property¶
Gets or sets the order in which this interceptor executes relative to other interceptors on the same target. Lower values execute first (outermost in the interceptor chain). Default is 0.
Property Value¶
Remarks¶
Example with Order values: - Order = 1: LoggingInterceptor (executes first, wraps everything) - Order = 2: CachingInterceptor (executes second) - Order = 3: ValidationInterceptor (executes last, closest to actual method)