New object creation on runtime

22 views
Skip to first unread message

Nik

unread,
Apr 8, 2015, 2:09:10 PM4/8/15
to sil...@googlegroups.com
Hello!


While resolving dependency for some MyClass, I want to create new MyClass object, with constructor parameters given.

It may look like this:
 IOC.resolve(MyClass.class, "abc", 12);
where IOC is my ioc wrapper,
MyClass is class to resolve,
"abc" and 12 are constructor parameters.

After creation container saves this new object. And for future requests with same parameters this saved object will be returned.


I successfully tried to use configured dependencies, but I can not come up with the implementation of this case.
Is it possible to achieve functionality described above?


Best regards,
Nik

Jan Bernitt

unread,
Apr 11, 2015, 3:45:25 AM4/11/15
to sil...@googlegroups.com
Hi Nik,

let me first say that it is the idea of the container to not mix up dependency management code with application code. 
Instead state from command line arguments or such is passed to the bootstrapping process. 
It can than be bound for particular constructor parameters using different methods.  

One would be to use presets. 
class Example1Module1 extends BinderModuleWith<Properties> {

@Override
protected void declare(Properties properties) {
bind(MyClass.class).toConstructor(
BoundParameter.constant(String.class, properties.getProperty("x")),
BoundParameter.constant(Integer.class, (Integer)properties.get("y")));
}
}

Properties props = new Properties();
props.put("x", "abc");
props.put("y", 12);
Presets presets = Presets.EMPTY.preset(Properties.class, props);
Globals globals = Globals.STANDARD.presets(presets);
Injector injector = Bootstrap.injector(Example1Module1.class, globals);

Instead of using the Properties as a general data map you could also have a special class holding the two values you want to bind.

Another way could be to use named instances:

class Example1Module2 extends BinderModule {

@Override
protected void declare() {
bind(MyClass.class).toConstructor(
instance(named("foo"), raw(String.class)),
instance(named("bar"), raw(int.class))
);
// the below may of course appear in any other module
bind(named("foo"), String.class).to("abc");
bind(named("bar"), int.class).to(12);
}
}

Injector injector = Bootstrap.injector(Example1Module2.class);

Remember that you can easily point out that type or instance you want to have injected for particular constructor arguments.
Using BoundParameter you could provide a Supplier that dynamically provides the values you want. 
Note that you do not have to specify all constructor arguments. These are just hints you could give to the container when you want something particular to be injected.

Note that there have been class name changes in latest release 0.8 you can read about here https://github.com/jbee/silk/blob/master/RELEASE_NOTES.md

Let me know if that was helpful or otherwise describe in more detail what it is you want and maybe even why.

Best regards
Jan

Reply all
Reply to author
Forward
0 new messages