Implementing Assisted Injection via Services

25 views
Skip to first unread message

o...@drilon.com

unread,
Mar 5, 2014, 11:58:53 PM3/5/14
to sil...@googlegroups.com
The comparisons page and a GitHub issue mention that Silk Services can be used to get the equivalent of Guice's "assisted injection", but I can't wrap my head around that claim.

The point of assisted injection is to automatically generate code that instantiates some object. But the Silk Service examples only show bindings to manually-created code that instantiate the return value. UserServices.login(...) in the introduction page creates the Success return value and MathService.square(...) in the unit tests computes the Long return value. I don't see where I could use the Services functionality to avoid boilerplate, since each of my "assisted injections" would require me to explicitly code the constructor calls.

jan

unread,
Mar 6, 2014, 2:19:00 AM3/6/14
to sil...@googlegroups.com
I am sure I didn't had boilerplate code generation in mind when I said that. As far as I remember the point I wanted to make was that you can have a service interface what has say 1 parameter:

interface Service<T> {
 
void doIt(T value);
}

when binding this to you actual implementation method this method can have one or more parameters of which all but the one of your interface will be figured out automatically according to your bind. Say you implemented it like this:

void doItImpl(Context c, Foo f, Bar b) {
 
//...
}

So you inject a Service<Foo> somewhere than you'll automatically get it backed by the implementation above having all the other parameters also injected. In that sense I see it having similarities to the assisted inject feature as you just pass to a call what isn't the job of the DI tool to inject. 

Note that the sequence of those parameters doesn't play a role so this is very refactoring-safe. The method (usually) is just determined by return type and having a parameter of the actual type parameter of your service. I say usually since you can customise this easily.  
With that in mind this is not appropriate to use with e.g. something like String. The whole idea of service methods is based on having one distinct parameter type per function.

Cheers, Jan
Reply all
Reply to author
Forward
0 new messages