d
use case?
d
a thing that needs autowire DI and etc., but all the magic injected bindings never change what they resolve to once wired up the first time, but the instance will maintain state that needs to be separate from other instances i am wondering if it is possible to not redo all the sometimes expensive hooks in a getInstance call when we know what the results will be
e
Lucee has
duplicate( obj, false )
which does a shallow copy. https://docs.lucee.org/reference/functions/duplicate.html
d
Oh ok, that is what I thought you were using it for.... I have asked this about wirebox and was told the functionality to "split off" a singleton isn't there and to do what elpete mentioned of getting the value, duplicating and then putting that value into the property. Or to make your dependency not a singleton and just get a bean for each instance necessary
d
i'll fiddle with it uncertain about the behavior here, or rather how to make both
c.getBar() !== c_dup.getBar()
c.getFoo() === c_dup.getFoo()
Copy code
component /*A*/ { property foo inject='somesingleton'; }
component /*B*/ extends=A { property bar inject='statefulInstance'; }
component /*C*/ extends=B { property baz inject='statefulInstance2'; }
c = getInstance("C")
c_dup = duplicate(c, false); // c.getBar() !== c_dup.getBar() ?
d
Hmmm for your example I would definitely go the transient bean approach instead.
Maybe extract all the "same" fields to another singleton and then the fields that need to be customized per instance can be on the transient cfc
s
duplicate()
is also dangerous in Lucee due to an ORM bug from forever ago that can cause funky things with Hibernate sessions even if your code isn't touching Hibernate
😨 1
😑 1
d
Definitely a use case for the ability to ask the factory for a transient instance of a singleton. But I don't think any cf factory supports that currently
d
well lets dial down the requirements
Copy code
component /*A*/ singleton {}
v = getInstance("A")
v._magic_isSingleton() // is this possible
d
If you trust your components that are singletons will always have the singleton (aka no manual mappings) attribute for wiring, then you could get that by calling getMetaData
d
no they might be marked using explicit calls to
asSingleton
d
Then I don't think wirebox injects any fancy functionality to know if the instance your currently working with is a singleton. You would have to ask the factory if the beanName you requested is a singleton
👀 1
b
the instance will maintain state that needs to be separate from other instances
That's not a singleton 🙂
d
it is an instance composed of singletons and non-singletons
b
To confirm what @deactivateduser was saying above. IF you know the name of the mapping in Wirebox i.e.
getInstance( 'myMappingName' )
, then it's possible to look up whether or not that mapping was a singleton inside WireBox's binder where the mappings are stored.
But, given a random CFC, it's not necessarily possible to know what WireBox mapping was used to create it.
You can certainly make a GUESS at what the mapping may have been by looking for a mapping with the same name, etc. But it's not going to be 100%
You could get fancy with some wirebodx interception points that added some pubic properties of your design to any CFC that was built as a singleton, but I'm not sure if that's a road worth going down. I'm still digesting your use case.
Trying to duplicate objects is typically wrought with danger and issues such that most people don't try it 🙂
Depending on your purposes, you may be able to use a memento pattern if you simply want to create a second CFC with the same state as the original. So you could do • ask wirebox for a fresh, new CFC instance • get the memento from the one you want to copy • set it into the new instance
@David Rogers
j
@David Rogers Is this a multi-tenant app? If would suggest using a custom scope to make that a “singleton” exclusive to the tenant: https://wirebox.ortusbooks.com/getting-started/getting-jiggy-wit-it/scoping Then use actual singletons for static, universal objects.