Skip to content

Latest commit

 

History

History
101 lines (72 loc) · 2.52 KB

injection.md

File metadata and controls

101 lines (72 loc) · 2.52 KB

Aspect Injector Docs

Injecting Aspects

In the AspectInjector, injection is done by applying the trigger attribute to a target. Trigger is created by defining .net attribute class and marking it with [Injection] attribute.

// defining aspect
[Aspect(Scope.Global)]
class LogAspect {}

// defining a trigger
[Injection(typeof(LogAspect))]
class Log : Attribute {}
//

After the trigger is defined you can apply it to the target:

class TestClass {
    [Log]
    public void DoSomething() {}
}

However the trigger is only an "attempt" to inject something into the target. When the trigger is fired for an aspect and the target then it is up to the aspect to decide if anything is injected. For instance if the aspect effects are configured to deal only with public members and the target doesn't have such then nothing is injected.

Multi-trigger

Triggers can be defined to inject multiple aspects at a time.

// defining log aspect
[Aspect(Scope.Global)]
class LogAspect {}

// defining measure execution time aspect
[Aspect(Scope.Global)]
class MeasureAspect {}

// defining a trigger
[Injection(typeof(LogAspect))]
[Injection(typeof(MeasureAspect))]
class LogAll : Attribute {}

Injection propagation

Trigger propagates injection to submembers of a target. This behaviour will be customizable in future versions.

[Log]
class TestClass {
    public void DoSomething() {}
    public void DoSomethingElse() {}
}

// equals to

class TestClass {
    [Log]
    public void DoSomething() {}
    [Log]
    public void DoSomethingElse() {}
}

// and in case your dll has the only class - equals to
[assembly: Log]

Triggers and parameters

Triggers can carry parameters which can be read by an Aspect to execute additional logic.

[Injection(typeof(LogAspect))]
class Log : Attribute {

    public Level Level { get; }

    public Log (LogLevel level){
        Level = level;
    }
}

class TestClass {
    [Log(Level.Warning)]
    public void DoSomething() {}
}

Injection suppression

If there is a need to skip injection in some cases, there is an attribute SkipInjection, which can annotate classes, constructors, methods, properties, and events. Once applied, it suppresses any injections to the target.

Please note that inheritance is not taken into account - applying the attribute to a base class doesn't suppress injections to its descendants.