ddi
Made in the European Union

ClassResolver

The dynamic decision hook — how DDI's ClassResolver picks an implementation per @Inject lookup.

Work in progress. This page is the canonical reference for the ClassResolver API. Full content lands with the SaaS-policy tutorial series (see the roadmap).

The contract

package com.svenruppert.ddi.implresolver;

public interface ClassResolver<T> {
  Class<? extends T> resolve(Class<T> interf);
}

Annotate the resolver with @ResponsibleFor(Service.class) so DDI knows which interface it claims. One ClassResolver per interface — competing claims are a hard error.

Minimum viable example

@ResponsibleFor(Service.class)
public class ServicePolicy implements ClassResolver<Service> {
  public Class<? extends Service> resolve(Class<Service> i) {
    return SecurityContext.current().plan() == Plan.ENTERPRISE
        ? PremiumService.class
        : BasicService.class;
  }
}

DDI calls resolve(...) every time the field is injected. That’s the defining property of the library — and the reason classic DI containers can’t do this cleanly.

See also

  • Resolution rules — how the resolver fits with producers and direct implementations.
  • Producers — when you need a factory instead of a class.
  • The full tutorial series under /tutorials/.