Open sandboxFocusImprove this doc

LAMA0703: The aspect target of [IntroduceDependency] must be a type or a type member

Severity Error
Reported by Metalama.Extensions.DependencyInjection
Message Cannot use the [IntroduceDependency] advice because the target declaration '<TargetDeclaration>' is not a type or a type member.

Cause

The IntroduceDependencyAttribute advice introduces the dependency into the type targeted by the aspect. When the aspect targets a member, such as a method, the dependency is introduced into the declaring type of that member.

Metalama reports LAMA0703 when the aspect's target has no such type, that is when the aspect is applied to the compilation (an assembly-level aspect) or to a namespace. In the message, <TargetDeclaration> is the target of the aspect.

In the following example, the Audit aspect derives from CompilationAspect and is applied to the assembly, so there's no type into which the _auditSink dependency can be introduced.

1using Metalama.Extensions.DependencyInjection;
2using Metalama.Framework.Aspects;
3
4namespace Doc.LAMA0703.Error;
5
6// A compilation aspect has no type into which the dependency could be introduced.
7public class AuditAttribute : CompilationAspect
8{
9    [IntroduceDependency]
10    private readonly IAuditSink _auditSink;
11}
12
1using Doc.LAMA0703.Error;
2
3// Error: the aspect is applied to the assembly, which isn't a type.
4[assembly: Audit]
5
6namespace Doc.LAMA0703.Error;
7
8public interface IAuditSink
9{
10    void Write( string message );
11}
12
13public partial class OrderService
14{
15    public void PlaceOrder() { }
16}
17

Resolution

Apply the aspect that introduces the dependency to types or type members:

  • Change the aspect into a type aspect, for instance by deriving it from TypeAspect, and apply it to each type that needs the dependency, as in the following example.
  • To apply the aspect to many types, keep it a type aspect and add it from a fabric instead of using an assembly-level attribute.

1using Metalama.Extensions.DependencyInjection;
2using Metalama.Framework.Aspects;
3
4namespace Doc.LAMA0703.Fixed;
5
6// A type aspect introduces the dependency into its target type.
7public class AuditAttribute : TypeAspect
8{
9    [IntroduceDependency]
10    private readonly IAuditSink _auditSink;
11}
12
Source Code
1namespace Doc.LAMA0703.Fixed;
2


3public interface IAuditSink
4{
5    void Write( string message );
6}
7
8// Fixed: the aspect is applied to a type.
9[Audit]
10public partial class OrderService
11{
12    public void PlaceOrder() { }
13}
14
Transformed Code
1using Metalama.Framework.RunTime;
2
3namespace Doc.LAMA0703.Fixed;
4
5public interface IAuditSink
6{
7    void Write(string message);
8}
9
10// Fixed: the aspect is applied to a type.
11[Audit]
12public partial class OrderService
13{
14    public void PlaceOrder() { }
15
16    private IAuditSink _auditSink;
17
18    public OrderService([AspectGenerated] IAuditSink? auditSink = null)
19    {
20        this._auditSink = auditSink ?? throw new System.ArgumentNullException(nameof(auditSink));
21    }
22}
23

See also