Skip to content

prefer-interface-over-concrete-injection-unity

Prefer the Interface — Never Reference a Concrete Class That Has One

Section titled “Prefer the Interface — Never Reference a Concrete Class That Has One”

Always-loaded. Binds constructor parameters, field/property types, and DI registration/resolution.

If an interface for a dependency exists, depend on the interface. Never name the concrete class — Foo(IDataManager dm) not Foo(DataManager dm); private readonly IDataManager dm; not DataManager; Resolve<IDataManager>() not Resolve<DataManager>(). AsSelf() registration does not license concrete injection.

No interface exists — in order. (1) Yours → extract it now, same change. (2) Submodule/UPM package → cannot add it locally (reproducible-on-every-machine-unity.md); quarantine it in exactly one composition-root class behind an interface you own, document the exception, PR upstream. (3) Sealed, no interface → same as (2).

Scope the search before claiming absence (negative-result-scope.md): grep -rn "interface I<Name>" Assets Packages Library/PackageCache. Omit Library/PackageCache/ and every UPM package reads as “no interface” — it only materializes once Unity has resolved it.

Why it compounds, the exception-remark template, the review test, worked audit: docs/prefer-interface-over-concrete-injection-unity.md.

reproducible-on-every-machine-unity.md · negative-result-scope.md (core) · code-conventions-unity.md § DI & Architecture · development-principles.md (core) § “Errors Over Silent Fallbacks”