Service binding based on condition

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Service binding based on condition

Nathan Quirynen
Hi,

When having just one implementation of a service interface the binding
is done like the following:

public static void bind(ServiceBinder binder) {
      binder.bind(SomeServiceInterface.class, SomeServiceImpl.class);
}

But now I need to bind the interface to one of multiple possible
implementations. The condition is just based on a symbol value.
How would I do this conditional binding?

Nathan


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Service binding based on condition

Pavel Chernyak
Hi there.

public static void bind(ServiceBinder binder) {
     binder.bind(SomeServiceInterface.class,
SomeServiceImpl.class).withId("someId");
}

And in @inject annotation you will require to specify "someId" as name.

On 10 August 2017 at 18:44, Nathan Quirynen <[hidden email]>
wrote:

> Hi,
>
> When having just one implementation of a service interface the binding is
> done like the following:
>
> public static void bind(ServiceBinder binder) {
>      binder.bind(SomeServiceInterface.class, SomeServiceImpl.class);
> }
>
> But now I need to bind the interface to one of multiple possible
> implementations. The condition is just based on a symbol value.
> How would I do this conditional binding?
>
> Nathan
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


--
С уважением,
Черняк Павел Александрович
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Service binding based on condition

Charles Roth-2
"Read The Friendly Manual" :-)

http://tapestry.apache.org/defining-tapestry-ioc-services.html, look for
the "Service Ids" section.



On 8/10/2017 12:38 PM, Pavel Chernyak wrote:

> Hi there.
>
> public static void bind(ServiceBinder binder) {
>       binder.bind(SomeServiceInterface.class,
> SomeServiceImpl.class).withId("someId");
> }
>
> And in @inject annotation you will require to specify "someId" as name.
>
> On 10 August 2017 at 18:44, Nathan Quirynen <[hidden email]>
> wrote:
>
>> Hi,
>>
>> When having just one implementation of a service interface the binding is
>> done like the following:
>>
>> public static void bind(ServiceBinder binder) {
>>       binder.bind(SomeServiceInterface.class, SomeServiceImpl.class);
>> }
>>
>> But now I need to bind the interface to one of multiple possible
>> implementations. The condition is just based on a symbol value.
>> How would I do this conditional binding?
>>
>> Nathan
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Service binding based on condition

Thiago H. de Paula Figueiredo
In reply to this post by Nathan Quirynen
On Thu, Aug 10, 2017 at 12:44 PM, Nathan Quirynen <
[hidden email]> wrote:

> Hi,
>

Hi!


>
> When having just one implementation of a service interface the binding is
> done like the following:
>
> public static void bind(ServiceBinder binder) {
>      binder.bind(SomeServiceInterface.class, SomeServiceImpl.class);
> }
>
> But now I need to bind the interface to one of multiple possible
> implementations. The condition is just based on a symbol value.How would I
> do this conditional binding?
>

Through a builder method:
public SomeServiceInterface buildSomeServiceInterface( ... ) {
    if (condition) {
        return new SomeServiceImpl1(...);
    }
    else {
        return new SomeServiceImpl2(...);
    }
}


>
> Nathan
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


--
Thiago
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Service binding based on condition

Nathan Quirynen
Thanks Thiago,

this is what I needed.


Op 10/08/2017 om 23:10 schreef Thiago H. de Paula Figueiredo:

> On Thu, Aug 10, 2017 at 12:44 PM, Nathan Quirynen <
> [hidden email]> wrote:
>
>> Hi,
>>
> Hi!
>
>
>> When having just one implementation of a service interface the binding is
>> done like the following:
>>
>> public static void bind(ServiceBinder binder) {
>>       binder.bind(SomeServiceInterface.class, SomeServiceImpl.class);
>> }
>>
>> But now I need to bind the interface to one of multiple possible
>> implementations. The condition is just based on a symbol value.How would I
>> do this conditional binding?
>>
> Through a builder method:
> public SomeServiceInterface buildSomeServiceInterface( ... ) {
>      if (condition) {
>          return new SomeServiceImpl1(...);
>      }
>      else {
>          return new SomeServiceImpl2(...);
>      }
> }
>
>
>> Nathan
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Service binding based on condition

Nathan Quirynen
In reply to this post by Pavel Chernyak
Hey thanks Pavel for your answer,

I need only one of the implementations to be injected everytime when
injecting "SomeServiceInterface" and don't want to decide when injecting
the service, so I don't think this would work for my use case.

I went with the service builder method as Thiago advised in his reply.

Nathan

Op 10/08/2017 om 18:38 schreef Pavel Chernyak:

> Hi there.
>
> public static void bind(ServiceBinder binder) {
>       binder.bind(SomeServiceInterface.class,
> SomeServiceImpl.class).withId("someId");
> }
>
> And in @inject annotation you will require to specify "someId" as name.
>
> On 10 August 2017 at 18:44, Nathan Quirynen <[hidden email]>
> wrote:
>
>> Hi,
>>
>> When having just one implementation of a service interface the binding is
>> done like the following:
>>
>> public static void bind(ServiceBinder binder) {
>>       binder.bind(SomeServiceInterface.class, SomeServiceImpl.class);
>> }
>>
>> But now I need to bind the interface to one of multiple possible
>> implementations. The condition is just based on a symbol value.
>> How would I do this conditional binding?
>>
>> Nathan
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...