Re: Best way to implement factory with Guice

2015-03-16 Thread Barak Yaish
Thanks. Is there a way to overcome the exhausting if-else if- else if 
structure, some magical way to support new crawlers in the system without 
register them in that factory?

On Monday, March 16, 2015 at 3:57:56 PM UTC+2, Laszlo Ferenczi wrote:

 Hi,

 There are many ways to solve this problem, one easy enough to understand:

 @Inject
 ProviderCrawlerOne crawlerOneProvider;

 @Inject
 ProviderCrawlerTwo crawlerTwo;


 public Crawler getCrawler(String url) {
 if (url...) {
 return crawlerOneProvider.get();
 }
 else {
 return crawlerTwoProvider.get();
 }
 }


 As an alternative you can inject the injector itself and get the instance 
 from it.

 Also a slightly more advanced, but much more elgant way is to use 
 AssistedInject (it's exectly for this use case) See the guice docs for more 
 info.

 PS: no need to make everything static, the whole point of the DI framework 
 that you don't need static anchors in your code.

 --
 L

 --
 L

 On Sun, Mar 15, 2015 at 4:37 PM, Barak Yaish barak...@gmail.com 
 javascript: wrote:

 Hi,

 Doing my first steps with Guice, I thought the fastest way to understand 
 it would migrate parts of existing application to Guice style. The 
 application is kind of web crawler, and I have factory creating crawlers 
 based on the input url:

 public static Crawler getCrawler( String url )
 {
 try
 {
 if( url.contains( www.site1.com ) )
 return new Site1( AppConfig.getInstance() );
 else if( url.contains( www.site2.com ) )
 return new Site2( AppConfig.getInstance() );
 }
 catch ( Exception e )
 {
 logger.error( failure, e );
 }

 return null;
 }


 Is the Guice version is only take the crawler instances from the injector 
 and injecting the AppConfig?

 public static Crawler getCrawler( String url )
 {
 try
 {
 if( url.contains( www.site1.com ) )
 return GuiceInjector.getInstance( Site1.class );
 else if( url.contains( www.site2.com ) )
 return GuiceInjector.getInstance( Site2.class );
 }
 catch ( Exception e )
 {
 logger.error( failure, e );
 }

 return null;
 }

 Is there a better and/or more elegant way? 

 Thanks!

 -- 
 You received this message because you are subscribed to the Google Groups 
 google-guice group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to google-guice...@googlegroups.com javascript:.
 To post to this group, send email to google...@googlegroups.com 
 javascript:.
 Visit this group at http://groups.google.com/group/google-guice.
 To view this discussion on the web visit 
 https://groups.google.com/d/msgid/google-guice/8911e587-58bd-4631-80a2-7a5d91007431%40googlegroups.com
  
 https://groups.google.com/d/msgid/google-guice/8911e587-58bd-4631-80a2-7a5d91007431%40googlegroups.com?utm_medium=emailutm_source=footer
 .
 For more options, visit https://groups.google.com/d/optout.




-- 
You received this message because you are subscribed to the Google Groups 
google-guice group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-guice+unsubscr...@googlegroups.com.
To post to this group, send email to google-guice@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/c809db73-6456-493c-8047-95543ee382ef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Best way to implement factory with Guice

2015-03-15 Thread Barak Yaish
Hi,

Doing my first steps with Guice, I thought the fastest way to understand it 
would migrate parts of existing application to Guice style. The application 
is kind of web crawler, and I have factory creating crawlers based on the 
input url:

public static Crawler getCrawler( String url )
{
try
{
if( url.contains( www.site1.com ) )
return new Site1( AppConfig.getInstance() );
else if( url.contains( www.site2.com ) )
return new Site2( AppConfig.getInstance() );
}
catch ( Exception e )
{
logger.error( failure, e );
}

return null;
}


Is the Guice version is only take the crawler instances from the injector 
and injecting the AppConfig?

public static Crawler getCrawler( String url )
{
try
{
if( url.contains( www.site1.com ) )
return GuiceInjector.getInstance( Site1.class );
else if( url.contains( www.site2.com ) )
return GuiceInjector.getInstance( Site2.class );
}
catch ( Exception e )
{
logger.error( failure, e );
}

return null;
}

Is there a better and/or more elegant way? 

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
google-guice group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-guice+unsubscr...@googlegroups.com.
To post to this group, send email to google-guice@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/8911e587-58bd-4631-80a2-7a5d91007431%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.