-1

This is a what is the best practice kind of a question.

I am working on a user login . Used bloc for login events like loginInProgress, UserNameValidation , PasswordValidation and LoginSuccess. Now after login I have huge set of data in terms of JSON response to store locally via database and shared preferences. I named this info sort of as session storage. This information have also multiple filter operations before storing in database or shared preferences.So means I have to first get relative data from the information then it will be ready to save. I am using repository pattern to get data. Now I am confused that should this information storage process or code will be written in repository file or it will be in same BLOC class of Login or it will be a separate Session Manager class.

1 Answers1

0

I think, there is no "right" solution to this problem, it depends on the current architecture of your app. However, this is how I would handle this:

  1. BLoC will call an AuthService that will handle all the required logic;
  2. AuthService will be responsible for:
    • Fetching data from the database - use the related repository for this;
    • Do all the related mapping;
    • Store whatever is needed in local storage (e.g. Shared preferences);
    • Return the needed object to BLoC.
  3. BLoC now has the needed data, so it's only responsible to set the right state.

This way, your BLoC only depends on the AuthService - it should be easy to test the BLoC logic.

AuthService depends on the repository (where you get the user data from) and also is responsible for handling all the related mapping as well as storing whatever is needed to SharedPreferences. Again, to test the AuthService, you would only need to mock the repository and the SharedPreferences instance.

mkobuolys
  • 4,499
  • 1
  • 11
  • 27