Skip to main content

The standard runtime for application use cases.

Project description

UseCaseCore

The standard runtime for application use cases.

UseCaseCore gives business actions one explicit, typed, transactional execution model across validation, state loading, policy checks, transitions, audit, idempotency, events, and side effects.

Why

Your API layer is standardized. Your data layer is standardized. Your service layer is still where business logic leaks into routes, model methods, jobs, and helpers.

UseCaseCore standardizes that missing layer without replacing FastAPI, SQLModel, SQLAlchemy, Postgres, Alembic, Oso, pytransitions, Temporal, or the stack you already use.

Install

pip install usecasecore

Core path

command
  -> validate
  -> check idempotency
  -> load state
  -> check policy
  -> check transitions
  -> open transaction
  -> apply changes
  -> write audit
  -> emit events
  -> queue side effects
  -> return result

Quick example

from examples.move_inventory import (
    InMemoryInventoryRepository,
    MoveInventoryCommand,
    MoveInventoryUseCase,
)
from usecasecore import InMemoryIdempotencyStore

repo = InMemoryInventoryRepository()
repo.set_balance(product_id="sku-1", bin_id="A", qty=10)
repo.set_balance(product_id="sku-1", bin_id="B", qty=1)

use_case = MoveInventoryUseCase(
    repository=repo,
    idempotency_store=InMemoryIdempotencyStore(),
)

result = use_case.execute(
    MoveInventoryCommand(
        request_id="req-1",
        idempotency_key="move-sku-1-A-B-4",
        product_id="sku-1",
        from_bin_id="A",
        to_bin_id="B",
        qty=4,
        moved_by_user_id="user-1",
        reason="rebalancing",
    )
)

assert result.success is True
assert result.source_qty_after == 6
assert result.dest_qty_after == 5

Current v0.1.0 surface

  • UseCase execution shell with validation, state loading, policy checks, transitions, transactions, audit, events, jobs, and idempotency.
  • Result wrapper for use cases that want to return metadata for default audit/event/job dispatch.
  • In-memory audit, event, job, and idempotency implementations for examples and tests.
  • Adapter protocols for policy engines, state machines, workflow engines, event buses, and job queues.
  • Canonical MoveInventory example with validation, repository state loading, policy checks, invariant checks, audit, events, jobs, and idempotency replay.

Where it fits

FastAPI
  -> Command model
  -> UseCaseCore
  -> Repositories / Session
  -> SQLModel / SQLAlchemy
  -> Postgres

Alembic evolves schema.

What it is not

  • not an API framework
  • not an ORM
  • not a database
  • not a migration tool
  • not a BPM suite
  • not a no-code workflow builder
  • not a universal rules engine

Repository layout

src/usecasecore/          core package
src/usecasecore/adapters/ adapter protocols
examples/move_inventory/ canonical example
docs/                     documentation stubs
tests/                    lifecycle and example tests
index.html                homepage

Docs and Examples

Release Process

Publishing is manual for now. Do not upload to PyPI until CI is green.

Build and check the package:

rm -rf build dist src/usecasecore.egg-info
python3 -m build
python3 -m twine check dist/*

For TestPyPI, upload with a TestPyPI token:

python3 -m twine upload --repository testpypi dist/*

Then verify install in a clean environment:

python3 -m venv /tmp/ucc-test
source /tmp/ucc-test/bin/activate
python3 -m pip install \
  --index-url https://test.pypi.org/simple/ \
  --extra-index-url https://pypi.org/simple \
  usecasecore
python3 -c "from usecasecore import Command, Result, ExecutionContext, UseCase; print('OK')"
deactivate

Only publish to PyPI after the TestPyPI install works.

Status

Early alpha moving toward v0.1.0. The core abstractions are intentionally small while the execution model settles.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

usecasecore-0.1.0a2.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

usecasecore-0.1.0a2-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file usecasecore-0.1.0a2.tar.gz.

File metadata

  • Download URL: usecasecore-0.1.0a2.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for usecasecore-0.1.0a2.tar.gz
Algorithm Hash digest
SHA256 9df40b5123febb47b04e4e4577ee7202f8c075cd3e88eee862a24610a3b502d5
MD5 8562d537994fbbeffbdf66a47648ad37
BLAKE2b-256 600c9cab320be243cc0544a059ec03c3e8f75c9fa9c97679ccea0e76502b5ab8

See more details on using hashes here.

File details

Details for the file usecasecore-0.1.0a2-py3-none-any.whl.

File metadata

  • Download URL: usecasecore-0.1.0a2-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for usecasecore-0.1.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 5c4fa9666a3f2b0cf76d63e3b7d2546d846e60e287dd91328bc0686950f36b2a
MD5 daaf64ddfeb0440673fd4231c13ac586
BLAKE2b-256 c820f3f08441ba17785e83a5a04a8f02788d4d8ea704aa144703b6df405e3678

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page