I have a question on service-time vs run-time factory instantiation, perhaps, but really more about general OOP in the context of Expressive.
I am refactoring some old code. I have repository layer class that accepts a
$quote_id as a constructor parameter, effectively tying a particular quote to the that repository instance.
I thought it was a good thing™, when I wrote it before Expressive, as it locks in the repository instance to a particular
Quote object via the
$quote_id. Another thing that I think is good is I do not have to specify
$quote_id in each and every method of that repository.
However … Expressive does not allow to instantiate a repository this way through constructor at service-time, since there is no Request object yet available, where it is the request that contains a given
$quote_id in my app.
One way to overcome this is to:
QuoteRepositoryFactoryaccessible at run-time, where I can instantiate my QuoteRepository at run-time, i.e. in a handler:
$quoteRepository = $quoteRepositoryFactory->getQuoteRepository($quote_id)
Or I could drop that concept and have a plain
QuoteRepositorymethod instantiated at service-time with all the DB trimmings, and at run-time supply
$quote_idto each and every method., i.e.
Also I note that I have wrestled with this before … In particular, way #2 was suggested by @ocramius:
My question here is: is one way preferred over another? Are they purely preference-based? Is there a good reason to go with one over the other? Is there any other way?