proteanhq / protean

Pragmatic Framework for Ambitious Applications
https://docs.proteanhq.com/
BSD 3-Clause "New" or "Revised" License
20 stars 10 forks source link

Establish Repository connections JIT #113

Closed subhashb closed 5 years ago

subhashb commented 5 years ago

RepositoryFactory should prepare adapter objects and establish connections just in time so that connections are kept open only for the necessary duration. Connections are let go after processing the request.

We should initialize a unit of work at the beginning of a request, retrieve repository objects with valid current connections, process the transaction, and commit the unit of work while letting go of active connections.

subhashb commented 5 years ago

There are two concerns mixed in the current Adapter class: The act of retrieving and letting of database connections and the database CRUD operations of each entity.

I propose we split the existing Adapter class into Provider and Repository.

Provider:

Repository:

Typical control flow for a single Entity operation (like one executed in a Python script or an iPython workbook) would be:

  1. Initialize Providers
  2. Register Model
  3. Call Op on Entity (Ex. Account.create)
  4. Fetch a fresh Repository object with a live connection
  5. Create a new Transaction
  6. Handover operation to Repository (Ex. Repository._create_object)
  7. Commit Transaction
  8. Return Entity data

Typical control flow for UseCase processing would be:

  1. Initialize Providers
  2. Register Model(s)
  3. Usecase.execute (Ex. CreateAccountUseCase.execute)
  4. Create a new Transaction
  5. Fetch one or more fresh Repository objects with a live connection
  6. Handover CRUD operations to each Repository (Ex. Repository._create_object)
  7. Commit Transaction
  8. Return Response

To create and commit transactions, we should use the Unit of Work pattern. That will come later.

abhishek-ram commented 5 years ago

Yes this does sound good but in order to implement such a workflow we should have the concept of an application like in celery, flask... this should make things standardized.

subhashb commented 5 years ago

Very true. I am trying to keep the framework stateless/application-free - let's see how long we can persist 😄 We will need to go the application route eventually, I guess.