const mixinafIoc::ServiceOverrides

afIoc::ServiceOverrides

(Service) - Contribute to override previously defined services. Use to override production services with test versions, or to replace 3rd party services with your own implementation.

Use the service Id to specify the original service to override. The contribution may one of:

  • Type: The type is autobuilt by IoC. (Useful for non-const services.)
  • |->Obj|: The func is called to create the service. (Useful for non-const services.)
  • Obj: The actual implementation.

For example, to override the PieAndChips service with an instance of PieAndMash:

static Void bind(ServiceBinder binder) {
  binder.bind(PieAndChips#)  // the original service
}

@Contribute { serviceType=ServiceOverrides# }
static Void contributeServiceOverrides(Configuration conf) {
  conf["myPod::PieAndChips"] = PieAndMash()
}

Or the contribution could also one of be:

conf["myPod::PieAndChips"] = PieAndMash#
conf["myPod::PieAndChips"] = |->Obj| { return PieAndMash() }
conf["myPod::PieAndChips"] = PieAndMash()

Obviously, the overriding type ( PieAndMash ) has to fit the original service type ( PieAndChips ).

Taking advantage of Type Coercion, you can use the service Type as the key:

@Contribute { serviceType=ServiceOverrides# }
static Void contributeServiceOverrides(Configuration conf) {
  conf[PieAndChips#] = PieAndMash()
}

Note you can only override the implementation, not the definition. Meaning you can not change a service's id, scope or proxy settings.

Also note that (using your override Id) someone else can override your override!

@since 1.2

@uses Configuration of Str:Obj (serviceId:overrideImpl)