E11043: method uses too many distinct injected types - extract to a dedicated component
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 1304 (How do I fix E11043 when a method injects too many distin...)
What went wrong
A single callable (method, operator, function, service operation, program) uses more than 4 distinct injected component types via method-local injection. This makes it difficult to understand and test - it has too many collaborators.
How to fix it
- Extract the method into a dedicated component with field injection
- Group related injected types into a facade component
- Split the method into smaller methods with fewer dependencies
See also
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E11043: method uses too many distinct injected types - extract to a dedicated component
#!ek9 defines module error.e11043.bad defines function orchestrate() <- rtn as Boolean? a as ServiceA! b as ServiceB! c as ServiceC! d as ServiceD! e as ServiceE! rtn: a.doA() and b.doB() and c.doC() and d.doD() and e.doE() defines component ServiceA as abstract doA() as abstract <- rtn as Boolean? default operator ? ServiceB as abstract doB() as abstract <- rtn as Boolean? default operator ? ServiceC as abstract doC() as abstract <- rtn as Boolean? default operator ? ServiceD as abstract doD() as abstract <- rtn as Boolean? default operator ? ServiceE as abstract doE() as abstract <- rtn as Boolean? default operator ?
Fixed: Let one injected facade coordinate the services
#!ek9 defines module error.e11043.good defines function orchestrate() <- rtn as Boolean? a as ServiceA! b as ServiceB! c as ServiceC! rest as Coordinator! rtn: a.doA() and b.doB() and c.doC() and rest.coordinate() defines component ServiceA as abstract doA() as abstract <- rtn as Boolean? default operator ? ServiceB as abstract doB() as abstract <- rtn as Boolean? default operator ? ServiceC as abstract doC() as abstract <- rtn as Boolean? default operator ? Coordinator as abstract coordinate() as abstract <- rtn as Boolean? default operator ?