The subject probably doesn’t match what I am trying to do, for some reason I am having a brain drain with this, too many trees I think.
Anyway, I have a module that has some models that I would like to use across other modules. The module needs to be instantiated with a few variables, what I was doing was this in the module config.
binder.map(“modelDSL@coreModule”).to("#moduleMapping#.model.someService")
.initWith( Id = variables.settings.variable1.id, idKey = variables.settings.variable1.key );
Then in another module, I would like to instantiate this with different init values and reference them by the DSL name, at a guess I am thinking this is not possible. But wanted to ask here in case I am going nuts and not seeing the forest from the trees.
What I have been doing so far is to change the DSL name based on each module, yet I am thinking that there is an easier way where I could use the same DSL but it pulls up the one for that module.
Suggestions!
Regards,
Andrew Scott
WebSite: http://www.andyscott.id.au/
Google+: http://plus.google.com/113032480415921517411
Right now, all modules share the same injector, therefore the same binder, and the same mappings. So if multiple modules register the same mapping name, the last one overwrites.
Now, what you can do is create additional WireBox injectors (turn off scope registration or change the key) and you can set the child injector’s parent to be the main injector for the app. Then “override” mappings in the child injector. When you ask an injector for an object and it doesn’t have it, it will ask its parent injector to get it so you have a hierarchy and the child injector can still get anything from the parent.
http://wiki.coldbox.org/wiki/WireBox.cfm#parentInjector
The original idea was that each module would have it’s own injector, but we never implemented that even though you could do it yourself manually.
Thanks!
~Brad
ColdBox Platform Evangelist
Ortus Solutions, Corp
E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com
Brad,
I am not sure I understand that, have you give an example of this.
The docs link I provided shows the config. Here is a simple working example. I create a dog.cfc and cat.cfc in my model folder.
injector1 = new coldbox.system.ioc.injector();
injector1.getBinder().map( ‘animal’ ).to( ‘model.Dog’ );
injector1.getBinder().map( ‘fleabag’ ).to( ‘model.Dog’ );
injector2 = new coldbox.system.ioc.injector();
injector2.setParent( injector1 );
injector2.getBinder().map( ‘animal’ ).to( ‘model.Cat’ );
writeDump( injector2.getInstance( ‘animal’ ) );
writeDump( injector2.getInstance( ‘fleabag’ ) );
Output:
<>
<>
Note that injector2 is a child to injector1. When I ask injector2 for “animal” it gives me its mapping even though injector1 also has a mapping for animal. but when I ask it for “fleabag” it defers to its parent injector for that mapping. You can also specify the parent injector in the binder config.
Of course, in your instance, you wouldn’t need to create injector1, it would just be the main WireBox injector for the app. Injector2 would be a custom injector you created in your module. Note, you would need to DIRECTLY ask that injector for instances, since general DI would still be handled by the app’s main injector.
Thanks!
~Brad
ColdBox Platform Evangelist
Ortus Solutions, Corp
E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com
But I am already doing that.
Module 1
binder.map(“modelDSL@Module1”).toDSL(“modelDSL@CoreModule”)
.initWith( Id = variables.settings.variable1.id, idKey = variables.settings.variable1.key );
Module 2
binder.map(“modelDSL@Module2”).toDSL(“modelDSL@CoreModule”)
.initWith( Id = variables.settings.variable1.id, idKey = variables.settings.variable1.key );
I was hoping that I would not have to add the above for each module, some how just be able to do something like
property name=“modelService” inject=“modelDSLCoreModule”; //but injected with the modules property settings
And the module would automatically know that it needs to inject the settings for that module, somehow, so that I don’t need to keep creating different DSL names.
Doh, I just realised that my way doesn’t even work. Any reason why this would not work?
binder.map(“modelDSL@Module2”).toDSL(“modelDSL@CoreModule”)
.initWith( Id = variables.settings.variable1.id, idKey = variables.settings.variable1.key );
It appears the initWith is being ignored.
InitArguments don’t work with DSL. Here’s the code in the injector.buildInstance()
case “dsl” : {
oModel = instance.builder.buildSimpleDSL( dsl=thisMap.getDSL(), targetID=thisMap.getName() ); break;
}
The init args are available in that method, but you can see they’re not passed along to the DSL builder.
I’m not actually sure why mappings defined by DSL don’t use init args, but it’s probably because most of the DSL namespaces are for pre-existing objects that aren’t created at the time you request them. (cachebox, logbox, and ColdBox bits) This would probably make a good ticket for a future enhancement since the model DSL should really get any initargs passed along.
Thanks!
~Brad
ColdBox Platform Evangelist
Ortus Solutions, Corp
E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com
Damn,
How easy would it be to modify now, the other way you mentioned will not work for me becasue it is assumed the model’s path is known. Which it’s not as the module could be installed in many places, therefore has no known path to do this now.
Suggestions?