You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Ferran Maylinch <fe...@gmail.com> on 2012/03/19 20:04:24 UTC

Dependencies in a parent service, repeated in children constructors

Hello,

We have 6 "child" services that extend a "parent" service class. This
parent service class has 3 dependencies that are only needed in methods
inside that parent class. How would we configure these services?

One solution would be to add 3 innecessary parameters to the constructor of
each "child" service:

public MyServiceChild1 extends MyServiceParent {

  // This dependencies would be repeated 6 times in classes
  // that don't need them since they are only needed in MyServiceParent

  private final MyOtherService1 s1;
  private final MyOtherService2 s2;
  private final MyOtherService3 s3;

  public MyServiceChild1 (
      MyOtherService1 s1, MyOtherService2 s2, MyOtherService s3) {
    this.s1 = s1;
    this.s2 = s2;
    this.s3 = s3;
  }

  ...
}

Maybe another solution would be to use @Inject in fields of MyServiceParent:

public MyServiceParent {

  @Inject private MyOtherService1 s1;
  @Inject private MyOtherService2 s2;
  @Inject private MyOtherService3 s3;

  ...
}

I don't know whether this is possible but anyway it is not recommended
since Tapestry documentation says: "Caution: injection via fields uses
reflection to make the fields accessible. In addition, it may not be as
thread-safe as using the constructor to assign to final fields."

So, what should we do?

Thank you very much,

Ferran

Re: Dependencies in a parent service, repeated in children constructors

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Mon, 19 Mar 2012 18:47:18 -0300, fmaylinch <fe...@gmail.com>  
wrote:

> Thank you Thiago,

:)

> I think I can also wrap those 3 services inside a "facade" service so  
> there is only one injected service in each child and not 3.
> But I wonder whether there is a solution with less code repetition and  
> where I can add more child services without them having to know about  
> those
> dependencies.

Now this isn't Tapestry-IoC-specific anymore at all, but a code  
architecture question. By the way, I see no code repetition in what you  
describe.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Dependencies in a parent service, repeated in children constructors

Posted by fmaylinch <fe...@gmail.com>.
Thank you Thiago,

I think I can also wrap those 3 services inside a "facade" service so there
is only one injected service in each child and not 3.

But I wonder whether there is a solution with less code repetition and where
I can add more child services without them having to know about those
dependencies.

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Dependencies-in-a-parent-service-repeated-in-children-constructors-tp5577972p5578445.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Dependencies in a parent service, repeated in children constructors

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Mon, 19 Mar 2012 16:04:24 -0300, Ferran Maylinch  
<fe...@gmail.com> wrote:

> Hello,

Hi!

> We have 6 "child" services that extend a "parent" service class. This
> parent service class has 3 dependencies that are only needed in methods
> inside that parent class. How would we configure these services?

Exactly the way you would in other scenarios: field inject (@Inject) or  
constructor injection (my preferred).

> One solution would be to add 3 innecessary parameters to the constructor  
> of each "child" service:

They aren't unnecessary at all. You're subclassing a class, let's call it  
P, and P needs X, Y and Z. The subclasses of P *are* P instances, so they  
do depend on P's dependencies. Just pass the parent dependencies in the  
super() call to the parent constructor.

class Child extends P {

	...
	public Child(X x, Y y, Z z, ChildDependency childDependency) {
		super(x, y, z);
		this.childDependency = childDependency;
	}

}

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org