Custom Services
Services are objects that are available to be injected into a handler's argument list. You can map a service on a Global or Request level.
Global Mapping
A Macaron instance implements the inject.Injector
interface, so mapping a service is easy:
Request-Level Mapping
Mapping on the request level can be done in a handler via *macaron.Context
:
Mapping values to Interfaces
One of the most powerful parts about services is the ability to map a service to an interface. For instance, if you wanted to override the http.ResponseWriter
with an object that wrapped it and performed extra operations, you can write the following handler:
In this way, your code can enjoy new custom service feature without any change. Plus, allow more custom implementations of same type of services.
Last updated