Is it acceptable to use method-chaining, when working with a service that is managed by a dependency injection framework (say HK2)?
I'm unsure if it is allowed to "cache" the instance, even if its only within the scope of the injection.
Example Service that creates a pizza:
@Service
public class PizzaService {
private boolean peperoni = false;
private boolean cheese = false;
private boolean bacon = false;
public PizzaService withPeperoni() {
peperoni = true;
return this;
}
public PizzaService withCheese() {
cheese = true;
return this;
}
public PizzaService withBacon() {
bacon = true;
return this;
}
public Pizza bake() {
// create the instance and return it
}
}
Here the service is injected into a JAX-RS resource:
@Path('pizza')
public class PizzaResource {
@Inject
PizzaService pizzaService;
@GET
public Response getPizza() {
Pizza pizza = pizzaService
.withPeperoni()
.withCheese()
.bake();
return Response.ok(pizza).build();
}
}