Author: limpbizkit
Date: Sun Nov 16 16:03:48 2008
New Revision: 676
Added:
wiki/ThrowingProviders.wiki
Log:
Created wiki page through web user interface.
Added: wiki/ThrowingProviders.wiki
==============================================================================
--- (empty file)
+++ wiki/ThrowingProviders.wiki Sun Nov 16 16:03:48 2008
@@ -0,0 +1,71 @@
+#summary One-sentence summary of this page.
+
+= Throwing Providers =
+
+Guice isn't very good at handling exceptions that occur during provision.
+ * Implementers of Provider can only throw !RuntimeExceptions.
+ * Callers of Provider can't catch the exception they threw, because it
may be wrapped in a !ProvisionException.
+ * Injecting an instance directly rather than a Provider can cause
creation of the injected object to fail.
+ * Exceptions cannot be advertised in the API.
+
+!ThrowingProviders are an alternative to Providers that allow a checked
exception to be thrown.
+ * API consistent with Provider.
+ * Scopable
+ * Standard binding DSL
+
+The !ThrowingProvider interface is similar to Provider, but with a generic
Exception type:
+{{{
+public interface ThrowingProvider<T,E extends Exception> {
+ T get() throws E;
+}
+}}}
+
+For each application exception, create an interface that extends
the !ThrowingProvider. For our news widget application, we created the
FeedProvider interface that throws a !FeedUnavailableException:
+{{{
+public interface FeedProvider<T> extends ThrowingProvider<T,
FeedUnavailableException> { }
+}}}
+After implementing our !WordlNewsFeedProvider and !SportsFeedProvider, we
bind them in our module using !ThrowingProviderBinder:
+{{{
+public static class FeedModule extends AbstractModule {
+ protected void configure() {
+ ThrowingProviderBinder.create(binder())
+ .bind(FeedProvuyider.class, BbcFeed.class)
+ .annotatedWith(WorldNews.class)
+ .to(WorldNewsFeedProvider.class)
+ .in(HourlyScoped.class);
+
+ ThrowingProviderBinder.create(binder())
+ .bind(FeedProvider.class, BbcFeed.class)
+ .annotatedWith(Sports.class)
+ .to(SportsFeedProvider.class)
+ .in(QuarterHourlyScoped.class);
+ }
+}
+}}}
+Finally, we can inject the !FeedProviders throughout our application code.
Whenever we call get(), the compiler reminds us to handle
the !FeedUnavailableException:
+{{{
+public class BbcNewsWidget {
+ private final FeedProvider<BbcFeed> worldNewsFeedProvider;
+ private final FeedProvider<BbcFeed> sportsFeedProvider;
+
+ @Inject
+ public BbcNewsWidget(
+ @WorldNews FeedProvider<BbcFeed> worldNewsFeedProvider,
+ @Sports FeedProvider<BbcFeed> sportsFeedProvider) {
+ this.worldNewsFeedProvider = worldNewsFeedProvider;
+ this.sportsFeedProvider = sportsFeedProvider;
+ }
+
+ public GxpClosure render() {
+ try {
+ BbcFeed bbcWorldNews = worldNewsFeedProvider.get();
+ BbcFeed bbcSports = sportsFeedProvider.get();
+ return NewsWidgetBody.getGxpClosure(bbcWorldNews, bbcSports);
+ } catch (FeedUnavailableException e) {
+ return UnavailableWidgetBody.getGxpClosure();
+ }
+ }
+}
+}}}
+==Notes on Scoping==
+Scopes work the same way they do with Providers. Each time get() is
called, the returned object will be scoped appropriately. Exceptions are
also scoped. For example, when worldNewsFeedProvider.get() throws an
exception, the same exception instance will be thrown for all callers
within the scope.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en
-~----------~----~----~----~------~----~------~--~---