I am looking into using DI for a couple of compone...
# box-products
r
I am looking into using DI for a couple of components that have an init function with required parameters. I can't believe that I have not come across this question before in previous projects with DI, but how does one use property injection to initialize a component but needing to also set parameters into property injection in order for the init function not to error? I looked through the docs, but there is only a short reference that I could find, which maybe suggests the use of a Constructor Injection, but I believe that means I would need to modify the component to be initialized by the DI property. I'm not sure if it is possible, but I would like to be able to just set parameters in the property injection, instead. Is this possible?
a
You move it out of the init and use
onDIComplete
(which can be a named method or use an annotation). Wirebox will call
onDIComplete
once everything is wired up.
You can also get Wirebox to inject your constructor arguments using the
inject
annotation. Something like
<cfargument inject="model" name="dsn" type="string" required="true">
I had to do that with some code we have which was written long before I added in Wirebox.
b
@ryan Yes, what John said above is correct
You can't use a mixin injection in your init and this is due to the order WireBox creates CFC in • CFC is instantiated via createOBject (which DOESN'T call init yet) • WireBox processes mixin (property) injections • WireBox calls your init, passing in any constructor args • WireBox calls the
onDIComplete()
method • The object is now built and returned That isn't all the steps, but it's the relevant ones. So your two options are as follows • inject the required dependencies to the init as constructor args (like John showed above) • Or move your init logic into an
onDIComplete()
method
r
Hey guys! Thank you for your responses and help on this!
If I'm understanding both you correctly (I have never used
onDIComplete()
before), I think I would prefer to use
onIComplete()
so that I will not need to modify the CFC getting instantiated. The reason why I don't want to modify the CFC is because I would like to keep the CFC versatile and without any dependencies regarding Coldbox for folks that do not use Coldbox. So, if I understand correctly, I need to call the init logic in
onDIComplete()
somewhere, which I gather would be in the handling CFC that was calling the mixin property injection. Instead of using a mixin, I create a
onDIComplete()
function that instantiates the injected cfc's init.
b
I'm a little confused-- you said you dont' want to modify the CFC, but isn't adding an onDIComplete method count as modifying it?
r
I'm speaking of two cfcs. I am wanting one cfc to call the other by DI. I need to place the onDIComplete method in the cfc that calls the other cfc, is that correct? I haven't had a chance to get back to this project, yet. I will read up on the method as soon as I can.
b
I need to place the onDIComplete method in the cfc that calls the other cfc, is that correct?
I'm really not sure which is which at this point to be honest. You need to place the onDIComplete method in the CFC which has instantiation logic which you need to run AFTER DI has been processed. So whichever CFC that applies to.... that's the one where you need it, lol
There are other things you do in Wirebox to map the constructor args of a CFC in the WireBox binder so you don't need to modify the CFC, but really without having a better understanding of your code it's hard to explain how you would use that.
r
Thanks, @bdw429s. This is a personal project, so when I get time to hop back on this, I will describe it in better detail, but not before I do what you and @aliaspooryorik have suggested already to do and I will catch up to get familiarized by reading about the onDIComplete method as well.