The current design of the CustomerIO class relies on static methods, making it difficult to write unit tests and achieve Dependency Injection. Converting static methods to instance methods will make the class more testable and flexible.
Proposed Changes:
Convert static methods in CustomerIO to instance methods.
Continue to use the existing private constructor const CustomerIO._(); to ensure that CustomerIO remains a singleton.
This approach will maintain the singleton behavior while offering greater flexibility for testing and modularization.
Example:
Before:
// Using static method
CustomerIO.subscribeToInAppEventListener(_handleInAppEvent);
After:
// Using instance method through singleton
final customerIO = CustomerIO.instance;
customerIO.subscribeToInAppEventListener(_handleInAppEvent);
Practical Example with InAppMessagesBloc:
Suppose you have an InAppMessagesBloc that handles incoming in-app messages. In the current architecture, mocking CustomerIO for testing the Bloc is difficult.
Description:
The current design of the
CustomerIO
class relies on static methods, making it difficult to write unit tests and achieve Dependency Injection. Converting static methods to instance methods will make the class more testable and flexible.Proposed Changes:
CustomerIO
to instance methods.const CustomerIO._();
to ensure thatCustomerIO
remains a singleton.This approach will maintain the singleton behavior while offering greater flexibility for testing and modularization.
Example:
Before:
After:
Practical Example with InAppMessagesBloc:
Suppose you have an
InAppMessagesBloc
that handles incoming in-app messages. In the current architecture, mockingCustomerIO
for testing theBloc
is difficult.Before:
After:
With the proposed changes, we can inject a mock or stubbed
CustomerIO
instance into theInAppMessagesBloc
for better unit testing.Benefits: