Class AspectAttribute
Defines an abstract aspect that can be applied against service interfaces or classes. Aspects are the prefered way to decorate service instances.
// Base class of all the validator attributes
public abstract class ParameterValidatorAttribute : Attribute
{
public abstract void Validate(ParameterInfo param, object value);
}
...
// Sample validator
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
public class NotNullAttribute : ParameterValidatorAttribute
{
public override void Validate(ParameterInfo param, object value)
{
if (value is null)
throw new ArgumentNullException(param.Name);
}
}
...
public class ParameterValidatorProxy : IInterfaceInterceptor
{
public ParameterValidator(IDependency dependency) {...}
public object? Invoke(IInvocationContext context, CallNextDelegate<IInvocationContext?, object> callNext)
{
foreach (var descr in context.TargetMethod.GetParameters().Select
(
(p, i) => new
{
Parameter = p,
Value = context.Args[i],
Validators = p.GetCustomAttributes<ParameterValidatorAttribute>()
}
))
{
foreach (var validator in descr.Validators)
{
validator.Validate(descr.Parameter, descr.Value);
}
}
return callNext(context);
}
}
...
// Define the comcrete aspect
[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class, AllowMultiple = false)]
public sealed class ParameterValidatorAspect : AspectAttribute
{
public ParameterValidatorAspect(): base(typeof(ParameterValidatorProxy)) { }
}
// Then annotate the desired interface ...
[ParameterValidatorAspect]
public interface IService
{
void DoSomething([NotNull] object arg);
}
// ... OR class (recommended)
[ParameterValidatorAspect]
public class Service : IService
{
// Only methods implementing the above declared interface can be annoteted
void DoSomething([NotNull] object arg) { ...}
}
Inheritance
System.Object
AspectAttribute
Namespace: Solti.Utils.DI.Interfaces
Assembly: Solti.Utils.DI.Interfaces.dll
Syntax
public class AspectAttribute : Attribute
Remarks
Constructors
AspectAttribute(Expression<CreateInterceptorDelegate>)
Creates a new AbstractServiceEntry instance.
Declaration
public AspectAttribute(Expression<CreateInterceptorDelegate> factory)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<CreateInterceptorDelegate> | factory |
AspectAttribute(Type)
Creates a new AbstractServiceEntry instance.
Declaration
public AspectAttribute(Type interceptor)
Parameters
Type | Name | Description |
---|---|---|
System.Type | interceptor |
AspectAttribute(Type, Object)
Creates a new AbstractServiceEntry instance.
Declaration
public AspectAttribute(Type interceptor, object explicitArgs)
Parameters
Type | Name | Description |
---|---|---|
System.Type | interceptor | |
System.Object | explicitArgs |
Properties
ExplicitArgs
Explicit arguments to be passed:
new {ctorParamName1 = ..., ctorParamName2 = ...}
Declaration
public object ExplicitArgs { get; }
Property Value
Type | Description |
---|---|
System.Object |
Factory
Factory function responsible for creating the concrete interceptor.
Declaration
public Expression<CreateInterceptorDelegate> Factory { get; }
Property Value
Type | Description |
---|---|
System.Linq.Expressions.Expression<CreateInterceptorDelegate> |
Interceptor
The underlying interceptor.
Declaration
public Type Interceptor { get; }
Property Value
Type | Description |
---|---|
System.Type |
Remarks
This type must be a class that can be instantiated and implements the IInterfaceInterceptor interface.