Deferred construction
- Created
- Decided
- Versions
- History
Abstract
I will remove Ghost and GhostResolver, so a lazy proxy is the only way the container
defers construction. A lazy proxy will never be cached, whatever the lifetime of the instance
behind it. A lazy resolution will not carry constructor arguments.
Motivation
The container defers construction in two ways. A dependency marked with Lazy receives a
lazy proxy. A dependency marked with Ghost receives a
ghost object from GhostResolver. Either way, the instance is constructed when the
object is first accessed.
A lazy proxy is a separate object from the instance behind it. When the lazy proxy is first accessed, it resolves the instance through the container. The instance is then cached and disposed of under its own lifetime. The lazy proxy itself is never cached, and the container keeps no reference to it. A lazy proxy that is never accessed constructs nothing.
A ghost object becomes the instance itself when its constructor runs. Under RFC-0007, a
ghost object follows its lifetime. It is cached as the instance before its constructor runs, and added to the
disposal list when it is initialised. A ghost object of a Process or Cycle lifetime
that was not cached would be a second instance beside the cached one, as RFC-0007 records.
Caching a ghost object before its constructor runs needs rules that a lazy proxy does not. Eager construction of the
ghost object's own class has to find the uninitialised ghost object in the cache and initialise it. A lazy proxy whose
resolution finds an uninitialised ghost object has to initialise the ghost object first. A ghost object of a Cycle
lifetime has to check the cycle when it is initialised, as well as when it is created.
A resolution can carry constructor arguments through with(). The arguments are used only
where the resolution constructs the instance. A lazy proxy resolves its instance when it is first accessed. Where an
instance is cached by then, the lazy proxy's arguments are not used, and nothing reports it. Whether they are used
depends on the order in which the instance's resolutions run.
Proposal
One way to defer construction
Ghost and GhostResolver will be removed. A lazy proxy will be the only object the
container gives in place of an instance whose construction is deferred. A
dependency whose construction should wait until it is first used will be marked with
Lazy, as RFC-0001 decides for a parameter. A class that is costly to
construct, and a class whose own dependencies are not yet available, are examples.
ProviderResolver, which RFC-0007 registers alongside GhostResolver, will be registered
without it.
RFC-0006 states that Ghost breaks a circular dependency. Lazy will break one
instead, as RFC-0006 describes for a lazy request whose entry is already on the
resolution stack.
ADR-0029 decides that a ghost object looks up its binding as a resolution does. With no ghost object, that part of the decision will have nothing to apply to. A lazy proxy's resolution already looks up its binding at the binding step.
Lazy proxies are never cached
A lazy proxy will not be cached, and will not be added to the disposal list, whatever the lifetime of the instance behind it. A lazy resolution that finds no instance in the instance cache will be given a new lazy proxy each time.
When a lazy proxy is first accessed, it will resolve the same resolution with the lazy proxy step skipped, as RFC-0006 describes. That resolution will run as every other resolution does. It will decide the instance's lifetime, read and write the instance cache, and add the instance to the disposal list. Two lazy proxies of a cached instance will therefore forward to the same instance.
A lazy proxy that is never accessed will construct nothing, and there will be nothing to dispose of. The container will hold no reference to it.
Under RFC-0007, a lazy resolution of a Cycle lifetime throws LifetimeException when it
is requested with no cycle open, or while one is closing. A lazy proxy will instead be created
whatever the state of the cycle. Its resolution will check the cycle when the lazy proxy is first accessed, as every
resolution of a Cycle lifetime does.
A lazy proxy is a separate object from the instance behind it, and will stay one after it is first accessed.
$proxy === $instance will be false. A method of the instance that returns $this will return the instance, not the
lazy proxy. A dependency marked with Lazy will therefore not be identical to the instance
it forwards to.
Arguments
A lazy resolution will not carry constructor arguments. lazily() will throw
InvalidResolutionException when the resolution has arguments. with() will throw the
same exception when it would give a lazy resolution arguments. with() given an empty list adds no arguments, and
will not throw. Under RFC-0006, named() and qualifiedBy() throw the same exception
for a resolution given both a name and a qualifier.
A resolution can also be lazy because its binding or class is lazy. Such a resolution with
arguments will throw InvalidResolutionException at the lazy proxy step. It will throw even for a class with no
properties that could trigger initialisation, which is given no lazy proxy.
RFC-0001 states that a class marked Lazy is resolved as a lazy proxy
regardless of any argument. A class marked Lazy and resolved with arguments will throw instead.
Alternatives considered
Keeping ghost objects under their lifetime. A ghost object would be cached as the instance before its constructor runs, as RFC-0007 decides. RFC-0018 set out the rules this needs. They cover the ghost object's creation, its initialisation, its disposal and the eager construction of its own class. I rejected this because those rules exist only because a ghost object has to be the cached instance. A lazy proxy defers the same construction and needs none of them.
Never caching ghost objects. A ghost object would be created for each dependency, and
discarded if it is never accessed, as a lazy proxy is. I rejected this because a ghost object becomes the instance when
its constructor runs. For a Process or Cycle lifetime, it would be a second instance
beside the cached one, as RFC-0007 records. Only a Transient lifetime would be safe.
Every resolution of a Transient lifetime constructs a new instance, so nothing else could hold the same instance as
the ghost object.
Keeping Ghost and Lazy for different reasons. Ghost would be for a class whose own dependencies are not yet
available, and Lazy for a class that is costly to construct. I rejected this because the container does the same
thing for both. It constructs the instance when the object is first accessed, and the reason for deferring changes
nothing in the resolution.
Checking the cycle when a lazy proxy is requested, as RFC-0007 decides. A lazy
resolution of a Cycle lifetime would throw when requested with no
cycle open, or while one is closing. I rejected this because a lazy proxy has no lifetime of its
own. Only the instance behind it does, and that instance is resolved when the lazy proxy is first accessed.
Using a lazy resolution's arguments where its lazy proxy constructs the instance. The arguments would be used where no instance is cached when the lazy proxy is first accessed. I rejected this because whether they were used would depend on the order in which the instance's resolutions ran. Arguments that were not used would be dropped without being reported.
Constructing eagerly a resolution with arguments whose binding or class is lazy. The arguments would take precedence
over the laziness, and the instance would be constructed with them and cached under its
lifetime. I rejected this because Lazy on a class would then not decide how the class is
resolved. Whether it was given a lazy proxy would depend on each caller.
Throwing only where a lazy proxy would be created. A resolution with arguments whose class has no properties that
could trigger initialisation is constructed eagerly, so its arguments would be accepted. I rejected this because
whether a resolution with arguments threw would then depend on the properties of the class. A resolution made lazy
through lazily() throws whatever the class's properties are.
Backwards compatibility
These will change for code that resolves through the container today:
GhostandGhostResolverwill be removed. A parameter marked withGhostwill be marked withLazyinstead, and receive a lazy proxy in place of a ghost object. TodayGhostResolvergives it a ghost object whereGhostResolveris registered. Otherwise it throwsDependencyResolutionException, withInvalidResolverExceptionas its previous exception.- A lazy resolution of a
Cyclelifetime requested with no cycle open, or while one is closing, will be given a lazy proxy. Today it throwsLifetimeException. lazily()on a resolution with arguments, andwith()on a lazy resolution, will throwInvalidResolutionException. Today both are accepted, and the container never reads a resolution's arguments.- A resolution with arguments whose binding or class is lazy will throw
InvalidResolutionException. Today it is given a lazy proxy, or resolved eagerly for a class with no properties that could trigger initialisation.
Open questions
Changelog
Source rfc/0019-deferred-construction.md · commit d6910e4 · exported