Skip to content

Instantly share code, notes, and snippets.

@kerimovscreations
Created February 23, 2022 05:45
Show Gist options
  • Save kerimovscreations/6fa896785931d8033c4a60bc961d86ba to your computer and use it in GitHub Desktop.
Save kerimovscreations/6fa896785931d8033c4a60bc961d86ba to your computer and use it in GitHub Desktop.
Migrate legacy project to Clean architecture

Migration steps

~ marked steps will be replaced with dependency injection in the following steps.

1. Define View models

Start to move all business logic from view controller into view models.

~ Initialize view models in the view controllers.

2. Define repositories

Start to define data-related methods (from view models) into repository interfaces based on domains.

Implement the repositories with a mix of local/remote data sources and data modifiers.

~ Declare the repositories as singletons.

~ Call the methods from repository singleton instances in view models.

2.1 Separate data sources

Separate the data sources into local and remote.

Local data sources will be used to cache data temporarily or persistent.

Remote data sources handle the network requests.

3. Define use cases

Start to combine the connected repository methods into use case classes.

~ Singleton instances of the repositories should be called in use cases,

View models should not be aware of the repositories, only use cases should be called.

~ Initialize the use case instances each time in view model methods.

4. Implement dependency injection

Start to divide the existing project structure into 3 modules: presentation, domain, data.

All UI components should be placed in the presentation module.

  • UI components can access only domain module components.
  • Data management should be handled over the use case classes.
  • Register view models as a factory.
  • Inject use cases as constructor parameters in view models.

Use cases, repository interfaces, and entity models should be placed in the domain module.

  • Domain module elements should be accessible from presentation and data modules.
  • Entity models should not be serializable.
  • Register use cases as a factory.
  • Inject repositories as constructor parameters in use cases.

All repository implementations and data sources should be placed in the data module.

  • Data sources should not be accessible from outside the module.
  • Data serialization, network connectivity, and local database access should be done in the data module.
  • Register local data source implementations as a singleton.
  • Register remote data source and repository implementations as a factory.
  • Inject data sources as constructor parameters in repositories.

In the following links you can find module separation tutorials:

In the following links, you can find starter projects:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment