E11041: multiple methods use the same injected type - promote to a facade component

Category: E11xxx Code quality and IR analysis · Reported in phase: PRE_IR_CHECKS · Classification: INJECTION_PROMOTE_TO_FACADE · All error codes

What went wrong

Multiple methods (3 or more) in the same component or class use the same injected component type. This suggests the dependency is central to the construct and should be declared once as a field rather than re-injected per method. (Services and traits are exempt - they cannot hold fields.)

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 1340 (Why does EK9 suggest promoting an injected type to a f...)

How to fix it

  1. If the type appears in 3+ methods, promote it to a single field
  2. Group related method-local injections into a facade component
  3. Evaluate if the construct has too many responsibilities

See also

  • E11041: promote to facade (this error)
  • E11040: excessive injection fields

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E11041: multiple methods use the same injected type - promote to a facade component

#!ek9
defines module error.e11041.bad

  defines component

    AuditService as abstract
      record() as abstract
        -> msg as String
      default operator ?

    OrderProcessor

      cancel()
        -> label as String
        audit as AuditService!
        audit.record(label)

      refund()
        -> label as String
        audit as AuditService!
        audit.record(label)

      submit()
        -> label as String
        audit as AuditService!
        audit.record(label)

      default operator ?

Fixed: Inject it once, as a field the methods share

#!ek9
defines module error.e11041.good

  defines component

    AuditService as abstract
      record() as abstract
        -> msg as String
      default operator ?

    OrderProcessor
      audit as AuditService!

      cancel()
        -> label as String
        audit.record(label)

      refund()
        -> label as String
        audit.record(label)

      submit()
        -> label as String
        audit.record(label)

      default operator ?

Questions and answers

These Q&A entries show E11041 and its fix:

Found a problem?

If EK9 reported E11041 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.