[coldbox-4.0.0] Injecting custom service with a property

Hi,

I have been experimenting with Wirebox and been reading through the documentation but have had limited success so far…
I am just trying to call the sayHello() function in WireboxTestService2, using wirebox instead of instantiating the cfc myself.
I have put comments besides the three ways i’ve been doing this (I am hoping to get the middle one working).

Thanks

My Model

component name = “WireboxTestService1” output = “false” accessors = “true” {
property name = “myTestService” inject = “model:myTestService”;

public string function greet(string name) {
// The regular way (creating an instance of the WireboxTestService2 component) ← obviously, this works
return new WireboxTestService2().sayHello(arguments.name);

// The wirebox way (injecting the WireboxTestService2 component as a property) ← claims that variable [MYTESTSERVICE] doesn’t exist
return myTestService.sayHello(arguments.name);

// Using the wirebox injector to request an instance ← this does work, so the binder is obviously working fine
return new coldbox.system.ioc.Injector(binder = “myApp.config.WireboxBinder”).getInstance(“myTestService”).sayHello(arguments.name);
}

}

My Binder

component name = “testBinder” extends = “coldbox.system.ioc.config.Binder” {

function configure() {
map(“myTestService”).asSingleton().to(“myApp.models.WireboxTestService2”);
}

}

How are you instantiating the “wireBoxtestSerive” CFC? it must also be created via WireBox in order for it to have its dependencies injected.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Hi Brad,

Loved your jokes about developers yesterday, during the coldbox connection about sass and grunt btw. Would love to hear those about php developers :wink:

I am instantiating my cfc in the normal way;

Test File (currently)

writeOutput(new models.WireBoxTestService1().greet("Jamie"));

So I have mapped this service in the binder as well and changed the way I’m creating it to;

writeOutput(new coldbox.system.ioc.Injector(binder = “myApp.config.WireboxBinder”).getInstance(“myService1”).greet(“Jamie”));

and now the middle method works fine!

Thanks alot! :slight_smile:

Glad to hear!

A couple of notes:

  • You don’t need to create the injector yourself. In any handler, view, layout, or interceptor, just call getinstance( ‘wireboxtestervice1’ )
  • Or for that matter, just inject wireboxtestservice1 into wherever it is you’re running this code.
  • If you do actually want to interact with the injector, it exists in a variable called “wirebox” automatically in place like views or handlers. Or it can be injected into a model with the injection DSL “wireBox”
    property name=“injector” inject=“wirebox”;
  • Just so you know, you don’t have to create a mapping for each object unless you like doing that. Models placed in your app’s “models” folder will be found automatically since that is the default scan location. Things like aliases or persistence controls can also be specified right inside the CFC itself:
    /models/foobar.cfc
    component alias=“myFoobar” singleton {}
    That CFC can now be injected with zero config required in your /config/WireBox.cfc

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com