dependency injection failed on model mapping

Hi,

I'm having trouble injecting dependencies using <cfproperty>.

CB Tracer says:
WARN 08:49:46.10 AM coldbox.system.ioc.Builder
The DSL dependency definition: {NAME={ContactService},
SCOPE={instance}, DSL={model}, REQUIRED={false}} did not produce any
resulting dependency

I use default conventions, and here's my model directory structure:

+ model
  + crm
    + ContactService.cfc

I've added a mapping to modelMappings.cfm as follows:

addModelMapping(path="crm.ContactService");

If I use
<cfproperty name="ContactService" inject="model" scope="instance">

When trying to reference it, system would throw an error as it is not
defined.

However if I explicitly use getModel("crm.ContactService"), it will
work, or if I move ContactService.cfc up a level (to the root of model
folder) <cfproperty> would work as well, no WARN message.

I have init constructor in ContactService.cfc.

What causes the problem?

Thank you!

WireBox doesn't use the modelMappings.cfm file and the addModelMapping() method. Instead it uses a WireBox.cfc file in your config directory. Your coldbox.cfc config file should have wirebox.enabled=true and wirebox.binder="config/WireBox".

You would probably want to do something like map("crm.ContactService").to("ContactService").asSingleton();

The default scan location for WireBox is the models directory, but it isn't recursive. That would explain why it is found when you move it up a level. It also explains why it works when you do getModel("crm.ContactService") since WireBox appends the partial instantiation path to the scan location to look for it.

Thanks!

~Brad

@Brad,

Your advice rocks! Now I get it working:

map(“ContactService”).to(“model.crm.ContactService”).asSingleton();

Thanks again!!

By the way if you want to you can use mapDirectory() to do more than one file as well.

Regards,

Andrew Scott

http://www.andyscott.id.au/

@andrew, thanks for the proactive tip! :slight_smile:

Good tip. Just remember, if you want the contactService to be a singleton, but you are using mapDirectory() to map all the CFCs in a directory, you can put singleton=“true” in the cfcomponent tag and WireBox will do the same thing that .asSingleton() does in the config file.

Thanks!

~Brad