alexeyinkin commented on code in PR #24957:
URL: https://github.com/apache/beam/pull/24957#discussion_r1131041017


##########
learning/tour-of-beam/frontend/lib/locator.dart:
##########
@@ -61,3 +64,12 @@ void _initializeState() {
     ),
   );
 }
+
+void _initializeServices() {
+  final googleAnalyticsService = GoogleAnalytics4Service(
+    propertyId: kAnalyticsUA,
+  );
+  GetIt.instance.registerSingleton<AnalyticsService>(
+    googleAnalyticsService,
+  );

Review Comment:
   Although the analytics classes are inside the shared `playground_components` 
package, the instantiation of the service is external to it. This gives us the 
future flexibility to:
   
   1. Change the implementations for Playground and ToB independently.
   2. Write tests that mock the analytics service.
   3. Use the shared Playground widgets in a new app that does not send 
analytics data by registering a null-object for analytics.
   
   There are a number of ways to implement this:
   1. Wrap Playground widgets in a `Provider` widget to provide them with a 
specific analytics service. But this gives no compile-time guarantee that the 
provider is in the tree and leads to bugs.
   2. Use a service locator of which `get_it` is the most popular one. So we 
did.
   
   > It isn't clear from this what is actually implementing the abstract 
AnalyticsService class without some digging.
   
   We need easy swap to a potential `SomethingElseAnalyticsService`, so this 
abstraction is on purpose. We could register the service to be read as 
`get<GoogleAnalyticsService>()`, but then swapping would affect dozens of 
locations.
   
   The promise `get_it` gives is that widgets no longer have to reference 
global variables or rely on `Provider`s. And this promise is fulfilled. 
`Provider` leads to even more digging while global variables bar the 3 
opportunities above.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to