I have run into a small problem with autowiring. I have a company
model object that can have a parent company. I have tried autowiring
the parentCompany as
<cfproperty name="parentCompany" type="model:company"
But I get an infinite recurrsion (i think) as each time the parent
company is instantiated it creates another parent and another etc.
Eventually I get:
coldfusion.runtime.EventHandlerException: Event handler exception.
I tried autowire_stopRecursion="model.domain.company" on the company
model object but this does not seem to work. Am I taking the wrong
approach to this?
What is it that you're trying to accomplish? In other words, if I have
a company object, what would you want that object to be able to do in
regard to its parent company object?
Well, stuff like myCompany.getParentCompany().getName() or
myCompany.getParentCompany().getChildCompanies() for instance.
I suppose I could create another model for ParentCompany that has no
parent, but that seems a bit pointless.
This is interesting because of circular dependencies, since the object you are trying to inject is the same. So what happens is a circular reference. Because every single company will have a company also, thus it never ends.
As you did not provide a suggested solution I suspect that your answer
is that I can't do this? =)
sorry, responded quickly.
Well, how would the system now when to stop?
Company model object to create > Company created > Company needs ParentCompany > ParentCompany created > ParentCompany needs a ParentCompany > Cycle.
As you can see, the cycle is never broken. because the model object always refers to a parent. THe solution would be to inject the parent manually maybe, because autowiring is not descriptive enough.
You may have to ask a second object to return that value for you to
get past the infinite recursion.
Maybe you could have say "company" object and a "companyService" object.
1. The "companyService" object is able to return "company" objects
2. That service is injected into the "company" objects
So when you call myCompany.getParentCompany(), the "getParentCompany"
function in the myCompany object uses the service to return the parent
I'm using Transfer but the proof of concept I made should be the same.
I have an order object and I've injected a orderService object into
it. I then have a function in the order that asks the orderService for
an order object by id and it's returning the same order object. If I
instead coded the service function to return the parent order object
of the input I pass in, then it'd return the parent instead of itself.
Is there a way to create conditional injections?
Because our "model" objects have no inheritance from Coldbox objects,
therefore no way to do getModel(). Obviously we could do injection on
the controller, create a facade to getModel(), but that seems
- Will B.
Thanks for the feedback Gabriel. That is essentially how I handled