Hey,

So, I actually went and joined the @Inject mailing lists, to see the reasoning for the injection proposals, and maybe contribute. Here is what is being suggested.

If a constructor is to be injected, it needs to be marked with @Inject. Like so:
@Inject
public FooService(OtherService test, SomeObject bar)
{
...
}

There is no need to mark OtherService as a @Service, since the argument is that each type will only have one scope (e.g. "service") anyway. So, if you went into OtherService you might see:
@Service
public interface OtherService {...}

Where @Service would be annotated with @Scope.

I am guessing that the main reason for this is DRY, i.e. why specify @Service on each injection point when you can do it once on the OtherService interface instead. In our case, that works *usually*, but sometimes you would indeed need to specify it, to e.g. differentiate between @This and @Uses.

For field injections, you can also ask whether it makes the code easier to read or not. With @Inject it would be:
@Inject
OtherService test;
and in Qi4j it would be:
@Service
OtherService test;

The amount if typing is the same, so the main difference is that in one case the scope is explicit and in the other it is implicit and you need to check the type to find out.

This is the main principal difference that I can see between Qi4j DI and @Inject: in Qi4j the scope is explicit at the injection points, and in @Inject it is implicit and given by the type.

My question is: what would YOU prefer, and why?

/Rickard

_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to