• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [ANN] WOInject 1.0
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ANN] WOInject 1.0


  • Subject: Re: [ANN] WOInject 1.0
  • From: Henrique Prange <email@hidden>
  • Date: Wed, 11 Apr 2012 18:10:18 -0300

Hi Ramsey,

Guice is very flexible and powerful. You can use the @Named annotation to distinguish between implementations:

class ComponentOne {
	@Inject @Named("Advanced")
	Service service;
}

... or create your own annotation for a more type-safe approach:

class ComponentTwo {
	@Inject @Basic
	Service service;
}

class AppModule {
	void configure() {
		bind(Service.class).annotatedWith(Names.named("Advanced")).to(AdvancedServiceImpl.class);
		bind(Service.class).annotatedWith(Basic.class).to(BasicServiceImpl.class);
	}
}

Guice also solves the "robot legs problem". Imagine there is only one implementation of the Service interface named ServiceImpl. You want two instances of ServiceImpl: one pointing to the service's URL and another pointing to a backup URL. You can use PrivateModules to solve this problem as described in the Guice's FAQ [1] (see question "How do I build two similar but slightly different trees of objects?").

[1]http://code.google.com/p/google-guice/wiki/FrequentlyAskedQuestions

Cheers,

Henrique

On 10/04/2012, at 23:28, Ramsey Gurley wrote:

>
> On Apr 7, 2012, at 8:07 PM, Henrique Prange wrote:
>
>> Suppose you have a Service interface and a BasicService implementation for it. This Service is required across your components. So you have something like this:
>>
>> class ComponentOne {
>> 	Service service = new BasicService();
>> }
>>
>> class ComponentTwo {
>> 	Service service = new BasicService();
>> }
>>
>> ...
>>
>> Then, the project evolves, and you develop a new AdvancedService implementation for the Service interface. You have to traverse the entire code base updating the instantiation of Service.
>>
>> On the other hand, if you use Guice, you can delegate the creation of Services to Guice, and inject the instances of Service when required. You write code like this:
>>
>> class ComponentOne {
>> 	@Inject
>> 	Service service;
>> }
>>
>> class ComponentTwo {
>> 	@Inject
>> 	Service service;
>> }
>>
>> ...
>>
>> And you configure a Guice Module to specify that you want an instance of BasicService (or an AdvancedService) when you need a Service.
>>
>> class AppModule extends AbstractModule {
>> 	void configure() {
>> 		bind(Service.class).to(BasicService.class);
>> 	}
>> }
>>
>> If you need to change the implementation class, you just have to redefine your module:
>>
>> class AppModule extends AbstractModule {
>> 	void configure() {
>> 		bind(Service.class).to(AdvancedService.class);
>> 	}
>> }
>>
>
> What if I want an advanced service on component one and a basic service on component two?
>
> Ramsey
>


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: [ANN] WOInject 1.0 (From: Paul Hoadley <email@hidden>)
 >Re: [ANN] WOInject 1.0 (From: Henrique Prange <email@hidden>)
 >Re: [ANN] WOInject 1.0 (From: "Ricardo J. Parada" <email@hidden>)
 >Re: [ANN] WOInject 1.0 (From: Henrique Prange <email@hidden>)
 >Re: [ANN] WOInject 1.0 (From: Ramsey Gurley <email@hidden>)

  • Prev by Date: Re: WOWODC 2012 and Project Wonder Boot Camp tickets now on sale!
  • Next by Date: Re: [ANN] WOInject 1.0
  • Previous by thread: Re: [ANN] WOInject 1.0
  • Next by thread: Re: [ANN] WOInject 1.0
  • Index(es):
    • Date
    • Thread