I'd like to provide constructor arguments via Guice to new Cayenne data objects, but it looks like cayenne just uses Class.newInstance() to instantiate new instances. Is there any way to have Cayenne use a provider to get a new instance instead? I'd need it to do this, for instance, when instantiating new instances during a fetch not just when I create a new instance.
To expand a little, I have a Maven project that has all the Cayenne models and core business logic. Then I have another Maven project with the API implementation. I want to be able to configure parameters in the API project and have the model use those parameters at runtime.
So, could do it with a constructor injection or a provides method - either one is fine.
Is there any way to do this?
public class Foo extends _Foo { //_Foo extends CayenneDataObject
private final String hey;
public Job(@Inject @Named("foo-job") String hey) {
super();
this.hey = hey;
}
}
Cayenne persistent objects are not managed via dependency injection (DI). DI is appropriate for relatively small number of services, not for a potentially huge number of persistent objects.
A more idiomatic way to pass some values to an object is via Cayenne callbacks and listeners. Callbacks can be mapped in the Modeler and can not have any references to the app outside the object. So for your case listeners can be a solution. They can be DI-managed and mapped in Guice.