You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tuscany.apache.org by Ramkumar R <ra...@gmail.com> on 2008/10/10 09:56:45 UTC

Should Tuscany support Injection of service references through Constructors

Hi All,
I came across this requirement, while working with Spring, but I believe
this topic also holds good for Java implementations.

Spring supports Constructor Injection by allowing to inject properties and
objects via the constructors. Not sure if Tuscany should support this
feature? As SCA+Spring depends on Tuscany runtime for binding and dependency
injections.

I believe currently Tuscany supports injection of service references through
getter & setter methods.

This question arises because the biggest benefit of setter injection is that
it allows for circular dependencies. Circular dependencies are actually
pretty common. If you have a UserService that needs to get information about
an employer from CompanyService and the CompanyService needs information
about employees from UserService, right there you have a circular
dependency. In general I find a lot of circular dependencies between service
methods, but with complex object models it is possible to have circular
dependencies elsewhere.

So considering the circular dependency issue, I believe it would be a bad
idea to support the same in Tuscany.

Like to hear from the community for more insight on this topic and how we
should go about it.

-- 
Thanks & Regards,
Ramkumar Ramalingam

Re: Should Tuscany support Injection of service references through Constructors

Posted by Simon Nash <na...@apache.org>.
Mike Edwards wrote:
> Ramkumar R wrote:
>> Hi All,
>> I came across this requirement, while working with Spring, but I 
>> believe this topic also holds good for Java implementations.
>>
>> Spring supports Constructor Injection by allowing to inject properties 
>> and objects via the constructors. Not sure if Tuscany should support 
>> this feature? As SCA+Spring depends on Tuscany runtime for binding and 
>> dependency injections.
>>
>> I believe currently Tuscany supports injection of service references 
>> through getter & setter methods.
>>
>> This question arises because the biggest benefit of setter injection 
>> is that it allows for circular dependencies. Circular dependencies are 
>> actually pretty common. If you have a UserService that needs to get 
>> information about an employer from CompanyService and the 
>> CompanyService needs information about employees from UserService, 
>> right there you have a circular dependency. In general I find a lot of 
>> circular dependencies between service methods, but with complex object 
>> models it is possible to have circular dependencies elsewhere.
>>
>> So considering the circular dependency issue, I believe it would be a 
>> bad idea to support the same in Tuscany.
>>
>> Like to hear from the community for more insight on this topic and how 
>> we should go about it.
>>
>> -- 
>> Thanks & Regards,
>> Ramkumar Ramalingam
> 
> Folks,
> 
> The SCA Java specification explicitly supports constructor based 
> injection of both properties and references, so Tuscany should support 
> this.
> 
>  From the OASIS SCA Java specification - sca-javacaa-1.1-spec-cd01.pdf
> Lines 94-97:
> 
> 2.1.4 @Reference
> Accessing a service using reference injection is done by defining a 
> field, a setter method parameter, or a constructor parameter typed by 
> the service interface and annotated with an @Reference annotation
> 
> - note the explicit description of "constructor parameter"
> 
> This is made clear in the formal definition of the @Reference annotation 
> in section 8.15 (line 1375).
> 
> The same applies to the @Property annotation (Section 8.14, line 1311).
> 
> 
> Currently, the SCA Spring specification says nothing about constructor 
> injection, but I have raised a number of issues against that 
> specification to fix this and other problems.
> 
> 
> 
> Yours,  Mike.
> 
> 
There are some use cases that need constructor injection.  If an
implementation requires an injected reference or property to be set
once only and never changed, constructor injection is a convenient
way to ensure this.

I don't think there is any problem with constructor injection and
circular dependencies.  In the example given, CompanyService instances
would be injected with UserService proxies, and UserService instances
would be injected with CompanyService proxies.  This does not create
circularity, because CompanyService and UserService instances don't
refer to each other directly.

   Simon



Re: Should Tuscany support Injection of service references through Constructors

Posted by Mike Edwards <mi...@gmail.com>.
Ramkumar R wrote:
> Hi All,
> I came across this requirement, while working with Spring, but I believe 
> this topic also holds good for Java implementations.
> 
> Spring supports Constructor Injection by allowing to inject properties 
> and objects via the constructors. Not sure if Tuscany should support 
> this feature? As SCA+Spring depends on Tuscany runtime for binding and 
> dependency injections.
> 
> I believe currently Tuscany supports injection of service references 
> through getter & setter methods.
> 
> This question arises because the biggest benefit of setter injection is 
> that it allows for circular dependencies. Circular dependencies are 
> actually pretty common. If you have a UserService that needs to get 
> information about an employer from CompanyService and the CompanyService 
> needs information about employees from UserService, right there you have 
> a circular dependency. In general I find a lot of circular dependencies 
> between service methods, but with complex object models it is possible 
> to have circular dependencies elsewhere.
> 
> So considering the circular dependency issue, I believe it would be a 
> bad idea to support the same in Tuscany.
> 
> Like to hear from the community for more insight on this topic and how 
> we should go about it.
> 
> -- 
> Thanks & Regards,
> Ramkumar Ramalingam

Folks,

The SCA Java specification explicitly supports constructor based injection of both properties and 
references, so Tuscany should support this.

 From the OASIS SCA Java specification - sca-javacaa-1.1-spec-cd01.pdf
Lines 94-97:

2.1.4 @Reference
Accessing a service using reference injection is done by defining a field, a setter method 
parameter, or a constructor parameter typed by the service interface and annotated with an 
@Reference annotation

- note the explicit description of "constructor parameter"

This is made clear in the formal definition of the @Reference annotation in section 8.15 (line 1375).

The same applies to the @Property annotation (Section 8.14, line 1311).


Currently, the SCA Spring specification says nothing about constructor injection, but I have raised 
a number of issues against that specification to fix this and other problems.



Yours,  Mike.





Re: Should Tuscany support Injection of service references through Constructors

Posted by Simon Laws <si...@googlemail.com>.
On Mon, Oct 13, 2008 at 6:33 PM, Ramkumar R <ra...@gmail.com> wrote:

> Thanks Simon & Raymond, for clarifying this and directing me to the specs.
>
> The specs clearly talks how to handle this situation as shown below.
>
> 259 Cyclic references between components may be handled by the container in
> one of two ways:
> 260
> 261 • If any reference in the cycle is optional, then the container may
> inject a null value during
> 262 construction, followed by injection of a reference to the target before
> invoking any service.
> 263 • The container may inject a proxy to the target service; invocation of
> methods on the proxy may
> 264 result in a ServiceUnavailableException
>
> Let me try out the same and let you know how it goes with Spring.
>
>
> On Fri, Oct 10, 2008 at 9:12 PM, Raymond Feng <en...@gmail.com> wrote:
>
>> Hi, Ram.
>>
>> SCA also supports constructor-based dependency injection. For example,
>>
>> @Constrcutor
>> public MyServiceImpl(@Reference OtherService service, @Property prop) {
>> ...
>> }
>>
>> Thanks,
>> Raymond
>>
>> From: Ramkumar R
>> Sent: Friday, October 10, 2008 12:56 AM
>> To: dev@tuscany.apache.org
>> Subject: Should Tuscany support Injection of service references through
>> Constructors
>>
>>
>>
>> Hi All,
>> I came across this requirement, while working with Spring, but I believe
>> this topic also holds good for Java implementations.
>>
>> Spring supports Constructor Injection by allowing to inject properties and
>> objects via the constructors. Not sure if Tuscany should support this
>> feature? As SCA+Spring depends on Tuscany runtime for binding and dependency
>> injections.
>>
>> I believe currently Tuscany supports injection of service references
>> through getter & setter methods.
>>
>> This question arises because the biggest benefit of setter injection is
>> that it allows for circular dependencies. Circular dependencies are actually
>> pretty common. If you have a UserService that needs to get information about
>> an employer from CompanyService and the CompanyService needs information
>> about employees from UserService, right there you have a circular
>> dependency. In general I find a lot of circular dependencies between service
>> methods, but with complex object models it is possible to have circular
>> dependencies elsewhere.
>>
>> So considering the circular dependency issue, I believe it would be a bad
>> idea to support the same in Tuscany.
>>
>> Like to hear from the community for more insight on this topic and how we
>> should go about it.
>>
>> --
>> Thanks & Regards,
>> Ramkumar Ramalingam
>>
>
>
>
> --
> Thanks & Regards,
> Ramkumar Ramalingam
>

Great thanks Ram, look forward to hearing more ;-)

Simon

Re: Should Tuscany support Injection of service references through Constructors

Posted by Ramkumar R <ra...@gmail.com>.
Thanks Simon & Raymond, for clarifying this and directing me to the specs.

The specs clearly talks how to handle this situation as shown below.

259 Cyclic references between components may be handled by the container in
one of two ways:
260
261 • If any reference in the cycle is optional, then the container may
inject a null value during
262 construction, followed by injection of a reference to the target before
invoking any service.
263 • The container may inject a proxy to the target service; invocation of
methods on the proxy may
264 result in a ServiceUnavailableException

Let me try out the same and let you know how it goes with Spring.

On Fri, Oct 10, 2008 at 9:12 PM, Raymond Feng <en...@gmail.com> wrote:

> Hi, Ram.
>
> SCA also supports constructor-based dependency injection. For example,
>
> @Constrcutor
> public MyServiceImpl(@Reference OtherService service, @Property prop) {
> ...
> }
>
> Thanks,
> Raymond
>
> From: Ramkumar R
> Sent: Friday, October 10, 2008 12:56 AM
> To: dev@tuscany.apache.org
> Subject: Should Tuscany support Injection of service references through
> Constructors
>
>
>
> Hi All,
> I came across this requirement, while working with Spring, but I believe
> this topic also holds good for Java implementations.
>
> Spring supports Constructor Injection by allowing to inject properties and
> objects via the constructors. Not sure if Tuscany should support this
> feature? As SCA+Spring depends on Tuscany runtime for binding and dependency
> injections.
>
> I believe currently Tuscany supports injection of service references
> through getter & setter methods.
>
> This question arises because the biggest benefit of setter injection is
> that it allows for circular dependencies. Circular dependencies are actually
> pretty common. If you have a UserService that needs to get information about
> an employer from CompanyService and the CompanyService needs information
> about employees from UserService, right there you have a circular
> dependency. In general I find a lot of circular dependencies between service
> methods, but with complex object models it is possible to have circular
> dependencies elsewhere.
>
> So considering the circular dependency issue, I believe it would be a bad
> idea to support the same in Tuscany.
>
> Like to hear from the community for more insight on this topic and how we
> should go about it.
>
> --
> Thanks & Regards,
> Ramkumar Ramalingam
>



-- 
Thanks & Regards,
Ramkumar Ramalingam

Re: Should Tuscany support Injection of service references through Constructors

Posted by Raymond Feng <en...@gmail.com>.
Hi, Ram.

SCA also supports constructor-based dependency injection. For example,

@Constrcutor
public MyServiceImpl(@Reference OtherService service, @Property prop) {
...
}

Thanks,
Raymond

From: Ramkumar R
Sent: Friday, October 10, 2008 12:56 AM
To: dev@tuscany.apache.org
Subject: Should Tuscany support Injection of service references through 
Constructors


Hi All,
I came across this requirement, while working with Spring, but I believe 
this topic also holds good for Java implementations.

Spring supports Constructor Injection by allowing to inject properties and 
objects via the constructors. Not sure if Tuscany should support this 
feature? As SCA+Spring depends on Tuscany runtime for binding and dependency 
injections.

I believe currently Tuscany supports injection of service references through 
getter & setter methods.

This question arises because the biggest benefit of setter injection is that 
it allows for circular dependencies. Circular dependencies are actually 
pretty common. If you have a UserService that needs to get information about 
an employer from CompanyService and the CompanyService needs information 
about employees from UserService, right there you have a circular 
dependency. In general I find a lot of circular dependencies between service 
methods, but with complex object models it is possible to have circular 
dependencies elsewhere.

So considering the circular dependency issue, I believe it would be a bad 
idea to support the same in Tuscany.

Like to hear from the community for more insight on this topic and how we 
should go about it.

-- 
Thanks & Regards,
Ramkumar Ramalingam 


Re: Should Tuscany support Injection of service references through Constructors

Posted by Simon Laws <si...@googlemail.com>.
On Fri, Oct 10, 2008 at 8:56 AM, Ramkumar R <ra...@gmail.com> wrote:

> Hi All,
> I came across this requirement, while working with Spring, but I believe
> this topic also holds good for Java implementations.
>
> Spring supports Constructor Injection by allowing to inject properties and
> objects via the constructors. Not sure if Tuscany should support this
> feature? As SCA+Spring depends on Tuscany runtime for binding and dependency
> injections.
>
> I believe currently Tuscany supports injection of service references
> through getter & setter methods.
>
> This question arises because the biggest benefit of setter injection is
> that it allows for circular dependencies. Circular dependencies are actually
> pretty common. If you have a UserService that needs to get information about
> an employer from CompanyService and the CompanyService needs information
> about employees from UserService, right there you have a circular
> dependency. In general I find a lot of circular dependencies between service
> methods, but with complex object models it is possible to have circular
> dependencies elsewhere.
>
> So considering the circular dependency issue, I believe it would be a bad
> idea to support the same in Tuscany.
>
> Like to hear from the community for more insight on this topic and how we
> should go about it.
>
> --
> Thanks & Regards,
> Ramkumar Ramalingam
>

Tuscany injects proxies into references rather than the target object itself
so we could leverage that to solve the cyclic reference case. The Java
Component Implementation Specification describes some cyclic reference
strategies around line 259.

I imagine the same consideration needs to be given to spring assuming that
the SCA Spring specification calls for this feature. The spec is so thin
it's difficult to tell. What does the code do at the moment?

Regards

Simon

Re: Should Tuscany support Injection of service references through Constructors

Posted by Dan Becker <da...@gmail.com>.
Ramkumar R wrote:
> I came across this requirement, while working with Spring, but I believe
> this topic also holds good for Java implementations.
> 
> Spring supports Constructor Injection by allowing to inject properties and
> objects via the constructors. Not sure if Tuscany should support this
> feature? As SCA+Spring depends on Tuscany runtime for binding and dependency
> injections.
> 
> I believe currently Tuscany supports injection of service references through
> getter & setter methods.
> 

Supporting properties on an object is a good thing. It seems to me 
injecting them at construction is a special case of injecting them via a 
setter, namely the former is setting some properties, albeit at object 
creation time. I always thought it was a good thing to have a formal 
method which sets properties and always have the special constructor 
call that method.

-- 
Thanks, Dan Becker