You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Dmitry Gusev <dm...@gmail.com> on 2012/08/09 19:38:25 UTC

Inject all service implementations using Tapestry IoC

Hi,

I have several implementations of Worker interface:

        binder.bind(Worker.class, GitCloneWorker.class).withId(GIT_CLONE);

        binder.bind(Worker.class, GitPullWorker.class).withId(GIT_PULL);

Right now I'm doing this:

public static WorkerManager buildWorkerManager(

            @InjectService(GIT_CLONE) Worker gitCloneWorker,

            @InjectService(GIT_PULL) Worker gitPullWorker) {

        WorkerManager workerManager = new WorkerManagerImpl();

        workerManager.registerWorker(gitCloneWorker);

        workerManager.registerWorker(gitPullWorker);

        return workerManager;

    }

But I'd like to inject them all at once like this:

    public static WorkerManager buildWorkerManager(Collection<Worker>
workers) {

        WorkerManager workerManager = new WorkerManagerImpl();

        for (Worker worker : workers) {

            workerManager.registerWorker(worker);

        }

        return workerManager;

    }

This code will even run, but workers would be an empty collection.

Is it possible?

-- 
Dmitry Gusev

AnjLab Team
http://anjlab.com

Re: Inject all service implementations using Tapestry IoC

Posted by Dmitry Gusev <dm...@gmail.com>.
Hello, Robert!

This looks like what I need!

Now I can remove bind() calls and just do:

    @Contribute(WorkerManager.class)

    public static void defineWorkers(Configuration<Worker> conf) {

        conf.addInstance(GitPullWorker.class);

        conf.addInstance(GitCloneWorker.class);

    }

And I even don't need the buildWorkerManager method, since I can just

binder.bind(WorkerManager.class, WorkerManagerImpl.class);

and get all workers in constructor.

    public WorkerManagerImpl(Collection<Worker> workers) {

        this.workers = new ArrayList<Worker>();

        for (Worker worker : workers) {

            registerWorker(worker);

        }

    }

This is cool, thanks!

On Thu, Aug 9, 2012 at 10:05 PM, Robert Zeigler <robert.zeigler@roxanemy.com
> wrote:

> Hi Dimitry.
>
> What you need is to contribute your workers to the worker manager.
> Something like:
>
> contributeWorkerManager(Configuration<Worker> conf) {
>   conf.add(...)
>   conf.add(...)
> }
>
> Now, the useful thing about this is that any module that is used in your
> app that has a "contributeWorkerManager" will be called and all of the
> conf.adds will be conglomerated into the single collection which will be
> passed to your buildWorerManager method.
>
> So the missing piece for you is just the contributeWorkerManager. Or you
> could use annotations for better type-safety:
>
> @Contribute(WorkerManager.class)
> configWorkerManager(Configuration<Worker> conf) {
>   ...
> }
>
> Robert
>
> On Aug 9, 2012, at 8/912:57 PM , Dmitry Gusev wrote
>
> > Yes, thanks for correction. But you get the idea?
> >
> > How can I fix the signature to get all the implementations?
> >
> > On Thu, Aug 9, 2012 at 9:52 PM, Thiago H de Paula Figueiredo <
> > thiagohp@gmail.com> wrote:
> >
> >> On Thu, 09 Aug 2012 14:38:25 -0300, Dmitry Gusev <
> dmitry.gusev@gmail.com>
> >> wrote:
> >>
> >> Hi,
> >>>
> >>
> >> Hi!
> >>
> >>
> >> But I'd like to inject them all at once like this:
> >>>
> >>>    public static WorkerManager buildWorkerManager(Collection<**Worker>
> >>> workers) {
> >>>
> >>
> >> This isn't dependency injection, it's receiving distributed
> configuration,
> >> hence the empty collection.
> >>
> >> --
> >> Thiago H. de Paula Figueiredo
> >>
> >>
> ------------------------------**------------------------------**---------
> >> To unsubscribe, e-mail: users-unsubscribe@tapestry.**apache.org<
> users-unsubscribe@tapestry.apache.org>
> >> For additional commands, e-mail: users-help@tapestry.apache.org
> >>
> >>
> >
> >
> > --
> > Dmitry Gusev
> >
> > AnjLab Team
> > http://anjlab.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Dmitry Gusev

AnjLab Team
http://anjlab.com

Re: Inject all service implementations using Tapestry IoC

Posted by Robert Zeigler <ro...@roxanemy.com>.
Hi Dimitry. 

What you need is to contribute your workers to the worker manager. Something like:

contributeWorkerManager(Configuration<Worker> conf) {
  conf.add(...)
  conf.add(...)
}

Now, the useful thing about this is that any module that is used in your app that has a "contributeWorkerManager" will be called and all of the conf.adds will be conglomerated into the single collection which will be passed to your buildWorerManager method.

So the missing piece for you is just the contributeWorkerManager. Or you could use annotations for better type-safety:

@Contribute(WorkerManager.class)
configWorkerManager(Configuration<Worker> conf) {
  ...
}

Robert

On Aug 9, 2012, at 8/912:57 PM , Dmitry Gusev wrote

> Yes, thanks for correction. But you get the idea?
> 
> How can I fix the signature to get all the implementations?
> 
> On Thu, Aug 9, 2012 at 9:52 PM, Thiago H de Paula Figueiredo <
> thiagohp@gmail.com> wrote:
> 
>> On Thu, 09 Aug 2012 14:38:25 -0300, Dmitry Gusev <dm...@gmail.com>
>> wrote:
>> 
>> Hi,
>>> 
>> 
>> Hi!
>> 
>> 
>> But I'd like to inject them all at once like this:
>>> 
>>>    public static WorkerManager buildWorkerManager(Collection<**Worker>
>>> workers) {
>>> 
>> 
>> This isn't dependency injection, it's receiving distributed configuration,
>> hence the empty collection.
>> 
>> --
>> Thiago H. de Paula Figueiredo
>> 
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.**apache.org<us...@tapestry.apache.org>
>> For additional commands, e-mail: users-help@tapestry.apache.org
>> 
>> 
> 
> 
> -- 
> Dmitry Gusev
> 
> AnjLab Team
> http://anjlab.com


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


Re: Inject all service implementations using Tapestry IoC

Posted by Dmitry Gusev <dm...@gmail.com>.
Yes, thanks for correction. But you get the idea?

How can I fix the signature to get all the implementations?

On Thu, Aug 9, 2012 at 9:52 PM, Thiago H de Paula Figueiredo <
thiagohp@gmail.com> wrote:

> On Thu, 09 Aug 2012 14:38:25 -0300, Dmitry Gusev <dm...@gmail.com>
> wrote:
>
>  Hi,
>>
>
> Hi!
>
>
>  But I'd like to inject them all at once like this:
>>
>>     public static WorkerManager buildWorkerManager(Collection<**Worker>
>> workers) {
>>
>
> This isn't dependency injection, it's receiving distributed configuration,
> hence the empty collection.
>
> --
> Thiago H. de Paula Figueiredo
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.**apache.org<us...@tapestry.apache.org>
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Dmitry Gusev

AnjLab Team
http://anjlab.com

Re: Inject all service implementations using Tapestry IoC

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Thu, 09 Aug 2012 14:38:25 -0300, Dmitry Gusev <dm...@gmail.com>  
wrote:

> Hi,

Hi!

> But I'd like to inject them all at once like this:
>
>     public static WorkerManager buildWorkerManager(Collection<Worker>
> workers) {

This isn't dependency injection, it's receiving distributed configuration,  
hence the empty collection.

-- 
Thiago H. de Paula Figueiredo

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


Re: Inject all service implementations using Tapestry IoC

Posted by Norman Franke <no...@myasd.com>.
I'll post that as a separate thread to make it easier to find later.

Norman Franke
Answering Service for Directors, Inc.
www.myasd.com



On Aug 9, 2012, at 2:08 PM, Dmitry Gusev wrote:

> Hello, Norman!
> 
> Sure, I'd like to see your approach.
> 
> On Thu, Aug 9, 2012 at 10:02 PM, Norman Franke <no...@myasd.com> wrote:
> 
>> If all the workers are in the same package you could make/Google a
>> PackageEnumerator class that uses a ClassLoader to get all of the classes
>> in the package, and then register those. I do this with my DAOs interface
>> and implementations to keep me from having to remember to add them to the
>> AppModule.
>> 
>> I can dig up some code if you are interested and assuming you can get an
>> instance of the service via the class.
>> 
>> Norman Franke
>> Answering Service for Directors, Inc.
>> www.myasd.com
>> 
>> 
>> 
>> On Aug 9, 2012, at 1:38 PM, Dmitry Gusev wrote:
>> 
>>> Hi,
>>> 
>>> I have several implementations of Worker interface:
>>> 
>>>       binder.bind(Worker.class, GitCloneWorker.class).withId(GIT_CLONE);
>>> 
>>>       binder.bind(Worker.class, GitPullWorker.class).withId(GIT_PULL);
>>> 
>>> Right now I'm doing this:
>>> 
>>> public static WorkerManager buildWorkerManager(
>>> 
>>>           @InjectService(GIT_CLONE) Worker gitCloneWorker,
>>> 
>>>           @InjectService(GIT_PULL) Worker gitPullWorker) {
>>> 
>>>       WorkerManager workerManager = new WorkerManagerImpl();
>>> 
>>>       workerManager.registerWorker(gitCloneWorker);
>>> 
>>>       workerManager.registerWorker(gitPullWorker);
>>> 
>>>       return workerManager;
>>> 
>>>   }
>>> 
>>> But I'd like to inject them all at once like this:
>>> 
>>>   public static WorkerManager buildWorkerManager(Collection<Worker>
>>> workers) {
>>> 
>>>       WorkerManager workerManager = new WorkerManagerImpl();
>>> 
>>>       for (Worker worker : workers) {
>>> 
>>>           workerManager.registerWorker(worker);
>>> 
>>>       }
>>> 
>>>       return workerManager;
>>> 
>>>   }
>>> 
>>> This code will even run, but workers would be an empty collection.
>>> 
>>> Is it possible?
>>> 
>>> --
>>> Dmitry Gusev
>>> 
>>> AnjLab Team
>>> http://anjlab.com
>> 
>> 
> 
> 
> -- 
> Dmitry Gusev
> 
> AnjLab Team
> http://anjlab.com


Re: Inject all service implementations using Tapestry IoC

Posted by Dmitry Gusev <dm...@gmail.com>.
Hello, Norman!

Sure, I'd like to see your approach.

On Thu, Aug 9, 2012 at 10:02 PM, Norman Franke <no...@myasd.com> wrote:

> If all the workers are in the same package you could make/Google a
> PackageEnumerator class that uses a ClassLoader to get all of the classes
> in the package, and then register those. I do this with my DAOs interface
> and implementations to keep me from having to remember to add them to the
> AppModule.
>
> I can dig up some code if you are interested and assuming you can get an
> instance of the service via the class.
>
> Norman Franke
> Answering Service for Directors, Inc.
> www.myasd.com
>
>
>
> On Aug 9, 2012, at 1:38 PM, Dmitry Gusev wrote:
>
> > Hi,
> >
> > I have several implementations of Worker interface:
> >
> >        binder.bind(Worker.class, GitCloneWorker.class).withId(GIT_CLONE);
> >
> >        binder.bind(Worker.class, GitPullWorker.class).withId(GIT_PULL);
> >
> > Right now I'm doing this:
> >
> > public static WorkerManager buildWorkerManager(
> >
> >            @InjectService(GIT_CLONE) Worker gitCloneWorker,
> >
> >            @InjectService(GIT_PULL) Worker gitPullWorker) {
> >
> >        WorkerManager workerManager = new WorkerManagerImpl();
> >
> >        workerManager.registerWorker(gitCloneWorker);
> >
> >        workerManager.registerWorker(gitPullWorker);
> >
> >        return workerManager;
> >
> >    }
> >
> > But I'd like to inject them all at once like this:
> >
> >    public static WorkerManager buildWorkerManager(Collection<Worker>
> > workers) {
> >
> >        WorkerManager workerManager = new WorkerManagerImpl();
> >
> >        for (Worker worker : workers) {
> >
> >            workerManager.registerWorker(worker);
> >
> >        }
> >
> >        return workerManager;
> >
> >    }
> >
> > This code will even run, but workers would be an empty collection.
> >
> > Is it possible?
> >
> > --
> > Dmitry Gusev
> >
> > AnjLab Team
> > http://anjlab.com
>
>


-- 
Dmitry Gusev

AnjLab Team
http://anjlab.com

Re: Inject all service implementations using Tapestry IoC

Posted by Norman Franke <no...@myasd.com>.
If all the workers are in the same package you could make/Google a PackageEnumerator class that uses a ClassLoader to get all of the classes in the package, and then register those. I do this with my DAOs interface and implementations to keep me from having to remember to add them to the AppModule.

I can dig up some code if you are interested and assuming you can get an instance of the service via the class.

Norman Franke
Answering Service for Directors, Inc.
www.myasd.com



On Aug 9, 2012, at 1:38 PM, Dmitry Gusev wrote:

> Hi,
> 
> I have several implementations of Worker interface:
> 
>        binder.bind(Worker.class, GitCloneWorker.class).withId(GIT_CLONE);
> 
>        binder.bind(Worker.class, GitPullWorker.class).withId(GIT_PULL);
> 
> Right now I'm doing this:
> 
> public static WorkerManager buildWorkerManager(
> 
>            @InjectService(GIT_CLONE) Worker gitCloneWorker,
> 
>            @InjectService(GIT_PULL) Worker gitPullWorker) {
> 
>        WorkerManager workerManager = new WorkerManagerImpl();
> 
>        workerManager.registerWorker(gitCloneWorker);
> 
>        workerManager.registerWorker(gitPullWorker);
> 
>        return workerManager;
> 
>    }
> 
> But I'd like to inject them all at once like this:
> 
>    public static WorkerManager buildWorkerManager(Collection<Worker>
> workers) {
> 
>        WorkerManager workerManager = new WorkerManagerImpl();
> 
>        for (Worker worker : workers) {
> 
>            workerManager.registerWorker(worker);
> 
>        }
> 
>        return workerManager;
> 
>    }
> 
> This code will even run, but workers would be an empty collection.
> 
> Is it possible?
> 
> -- 
> Dmitry Gusev
> 
> AnjLab Team
> http://anjlab.com