Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data layer refactor #1727

Open
jan-goral opened this issue Mar 23, 2021 · 0 comments
Open

Data layer refactor #1727

jan-goral opened this issue Mar 23, 2021 · 0 comments
Labels

Comments

@jan-goral
Copy link
Contributor

jan-goral commented Mar 23, 2021

SDD

Scratch the data layer

Motivation

Clean up and redesign a data layer of flank to make it more visible also easier to understand and scale.

Goals

  • Improve visibility and scalability of the data layer.
  • Separate domain logic from third-party APIs.
  • Prevent from leaking third-party API structures.
  • Reuse the old implementation by wrapping it into a new data API.

Non-Goals

  • Refactoring the old implementation (this will be done in the future steps)

Design

API

  • Consider api abstraction as Kotlin object or data class.
  • The api abstraction is a context of specific operations on data.
  • The api context is grouping all related structures or interfaces under its namespace.
  • There are no needs for creating nested packages inside the api package.

Adapter

  • Consider the adapter as a wrapper for third-party API.
  • The adapter must implement a related interface from the data layer.
  • Single adapter should represent a single operation of data structures.
  • The adapters have to be placed in the adapter root package.
  • The implementation of old API wrappers should be moved into nesting packages inside the adapter package.

Steps

  1. Add abstraction to the api package.
  2. Move all implementation that will be covered by abstraction to the proper sub-package of ftl.adapter.
  3. Add new implementation of added abstraction.
    • New implementation should just wrap the old implementation call.
    • New implementation should be placed inside the data layer root package.
    • Do not forget to expose the reference to implementation.
  4. Replace old API wrapper calls with new abstraction usage.

Example

The description of how to solve each similar issue in practice, using #1740 as an example.

Step 1

Add abstraction to the data package.

Create new file ftl/data/UserAuthorization.kt. With body:

package ftl.api

val requestUserAuthorization: UserAuthorization.Request get() = TODO()

object UserAuthorization {
    interface Request : () -> Unit
}

Step 2

Move all implementation that will be covered by abstraction to the proper subpackage of ftl.adapter.

Consider the:

  • ftl/gc/UserAuth.kt
  • ftl/config/Credentials.kt

As wrappers for third-party API, that could be easily adapted to the data interface.

Make sure the list above is complete, if not take also missing files into an account.

Move wrappers to dedicated sub-package in ftl.adapter.

  • Consider the package ftl.client.google for the wide scope of Google-related API wrappers.

Step 3

Add new implementation for added abstraction.

Create new file ftl/adapter/GoogleUserAuthorizationRequest.kt. With body:

package ftl.adapter

import ftl.client.google.UserAuth
import ftl.api.UserAuthorization

object GoogleUserAuthorizationRequest : UserAuthorization.Request, () -> UserAuthorization by {
    UserAuth().request()
    UserAuthorization
}

Update file ftl/data/UserAuthorization.kt with:

val requestUserAuthorization: UserAuthorization.Request get() = GoogleUserAuthorizationRequest

Step 4

Replace old API wrapper calls with new abstraction usage.

Update file ftl/domain/LoginGoogleAccount.kt with:

package ftl.domain

import ftl.api.requestUserAuthorization

interface LoginGoogleAccount

operator fun LoginGoogleAccount.invoke() {
    requestUserAuthorization()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant