شعار Usecase

Usecase

المجتمع
ewtryocb
usecase

Use when creating or editing a use case under src/application/v1/usecases/. Enforces the RequestBus + UseCase pattern (Request + UseCase in one file, @dataclass(slots=True), returns Result type) and the DBGateway transaction rules — never open two sessions in one use case, never touch self.database outside a context manager, never lazy-load relationships outside the session.

نظرة عامة

الناشرewtryocb
المستودعfastapi-claude-template
اسم المهارةusecase
النجوم
33
التفريعات
5
الملفات المرفقة
تعليمات فقط
الترخيصMIT
الروابط
  • تعليمات بصيغة Markdown

    ملف SKILL.md يُحمّله النموذج عند الحاجة، فلا يستهلك توكنات إلا عندما يَرِد طلب ذو صلة فعلًا.

  • تعمل مع أي نموذج لغوي كبير

    مهارات الذكاء الاصطناعي مكتوبة بصيغة Markdown وليست شيفرة خاصة بمزوّد معيّن، لذا تعمل مع GPT أو Claude أو Gemini أو Grok أو نموذج محلي.

  • مكتفية بذاتها

    كل ما يحتاجه النموذج موجود في التعليمات — لا ملفات إضافية لمزامنتها.

  • مفتوحة المصدر

    نشرها ewtryocb على GitHub. اطّلع على المصدر قبل تثبيتها.

التثبيت

ثبّت مهارة الذكاء الاصطناعي Usecase في TypingMind لاستخدامها مع أي نموذج لغوي كبير، أو أضفها إلى وكيل ذكاء اصطناعي آخر يقرأ SKILL.md.

1

التثبيت في TypingMind

يُثبّت TypingMind المهارة مباشرةً من مجلدها على GitHub — فيقرأ SKILL.md، ويجمع ملفات الموارد في حزمة، ويحفظ النتيجة محليًا.

  1. افتح التطبيق وانتقل إلى الإضافات → المهارات.
  2. اختر «تثبيت من GitHub».
  3. الصق رابط مجلد المهارة الموضّح أدناه وأكّد التثبيت.
    https://github.com/ewtryocb/fastapi-claude-template/tree/master/.claude/skills/usecase
  4. فعّل المهارة في كل محادثة تريد إتاحتها فيها.
الإضافات → المهارات → إضافة مهارة → من رابط GitHub، ثم الصق رابط المجلد واضغط على «متابعة».
2

التثبيت في وكيل ذكاء اصطناعي آخر

يمكن لأي وكيل ذكاء اصطناعي يقرأ تنسيق Agent Skills استخدام هذه المهارة — انسخ المجلد إلى مجلد المهارات الخاص بذلك الوكيل.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/ewtryocb/fastapi-claude-template.git /tmp/fastapi-claude-template
mkdir -p .claude/skills
cp -r /tmp/fastapi-claude-template/.claude/skills/usecase .claude/skills/usecase
أعد تشغيل Claude Code بعد النسخ ليتعرّف على المهارة الجديدة.

استخدمها في TypingMind

فعّل Usecase في أي محادثة على TypingMind وسيتولى النموذج الباقي. تتضمن مطالبة النظام اسمها ووصفها، وبمجرد ورود طلب ذي صلة، يُحمّل النموذج التعليمات الكاملة تلقائيًا — فلا تحتاج إلى استدعائها يدويًا، ولا تستهلك أي توكنات حتى تُستخدم فعليًا.

يُحمّل النموذج Usecase تلقائيًا بمجرد ورود طلب ذي صلة بها.

تعمل مع أي نموذج ذكاء اصطناعي

مهارات الذكاء الاصطناعي هي تعليمات بصيغة Markdown وليست شيفرة خاصة بمزوّد معيّن، لذا لا ترتبط Usecase بالنموذج الذي كُتبت له. ثبّتها مرة واحدة في TypingMind واستخدمها مع GPT-5 أو Claude أو Gemini أو Grok أو DeepSeek أو Mistral أو Llama أو نموذج محلي تشغّله بنفسك — كل ذلك باستخدام مفاتيح API الخاصة بك.

  • تُحمَّل عند الحاجة فقط

    تتضمن مطالبة النظام الاسم والوصف فقط. وتُجلب التعليمات عند أول طلب ذي صلة، لذا لا تترتب أي تكلفة على المهارة غير المستخدمة.

  • بدّل النماذج أثناء المحادثة

    لأن المهارة تعليمات وليست شيفرة، فإن تغيير النموذج لا يعطّلها — فالنموذج التالي يقرأ ملف SKILL.md نفسه.

تعليمات المهارة

هذا هو محتوى SKILL.md الذي يُحمّله النموذج. اقرأه قبل التثبيت — فالمهارة عبارة عن تعليمات سيتبعها نموذجك.

Writing use cases (src/application/v1/usecases/<domain>/)

A use case is the transport-agnostic unit of business logic. Endpoints, consumers, and scheduled tasks all call request_bus.send(Request(...)) and get back a Result. Use cases never know who called them.

One file, two classes

Every use case file defines exactly one Request class and exactly one UseCase class. Keep them in the same file — they are a pair.

python
# src/application/v1/usecases/widget/create.py
from dataclasses import dataclass
from typing import Annotated

import uuid_utils.compat as uuid
from pydantic import Field

from src.application.common.interfaces.usecase import UseCase
from src.application.common.request import Request
from src.application.v1.results import WidgetResult
from src.database.psql import DBGateway


class CreateWidgetRequest(Request):
    name: Annotated[str, Field(max_length=64)]
    owner_uuid: uuid.UUID


@dataclass(slots=True)
class CreateWidgetUseCase(UseCase[CreateWidgetRequest, WidgetResult]):
    database: DBGateway

    async def __call__(self, request: CreateWidgetRequest) -> WidgetResult:
        async with self.database:
            widget = (
                await self.database.widget.create(
                    name=request.name,
                    owner_uuid=request.owner_uuid,
                )
            ).result()
            return WidgetResult(**widget.as_dict())

Invariants

  1. Request inherits src.application.common.request.Request (Pydantic, frozen=True). Validation via Annotated[..., Field(...)]. Do not reuse HTTP Contract classes here — the Request describes the use-case input, not the HTTP input.
  2. UseCase inherits UseCase[RequestClass, ReturnType]. The base class auto-applies @dataclass(slots=True) — add your own @dataclass(slots=True) on the subclass too for mypy/readability.
  3. Dependencies are dataclass fields: database: DBGateway, hasher: Hasher, cache: StrCache, services: ServiceGateway, etc. Injected by Dishka via the request bus — do not instantiate manually.
  4. Returns a Result subclass from src/application/v1/results/ — never a Contract from presentation/, never a bare ORM model, never a dict.
  5. Register the use case in src/application/v1/usecases/__init__.py::setup_use_cases():
    python
    request_bus.register(widget.CreateWidgetRequest, widget.CreateWidgetUseCase)
  6. Re-export from src/application/v1/usecases/widget/__init__.py so the presentation/consumers layer can import from src.application.v1.usecases.widget import CreateWidgetRequest.

DBGateway — the transaction rule

This is the single most common place people break production. Read carefully.

DBGateway owns one session per request. It exposes two context managers. You pick exactly one per use case — never both, never twice.

Pattern 1 — read-only

python
async def __call__(self, request: SelectWidgetRequest) -> WidgetResult:
    async with self.database.manager.session:
        widget = (
            await self.database.widget.select(*request.loads, widget_uuid=request.widget_uuid)
        ).result()
        return WidgetResult(**widget.as_dict())
  • Use for pure reads: one or many select / exists / count calls, no mutations.
  • No transaction is opened — session.begin() is never called.
  • Build the Result via WidgetResult(**widget.as_dict()) — see "Mapping ORM → Result" below for why .model_validate(widget) is forbidden.
  • Keep construction inside the async with block by convention — it localizes session-touching code and keeps the pattern consistent even when the use case later grows logic that genuinely needs the open session.

Pattern 2 — write

python
async def __call__(self, request: CreateWidgetRequest) -> WidgetResult:
    async with self.database:
        widget = (
            await self.database.widget.create(
                name=request.name,
                owner_uuid=request.owner_uuid,
            )
        ).result()
        return WidgetResult(**widget.as_dict())
  • Use for any mutation (create / update / delete / upsert).
  • async with self.database: calls session.begin() → autocommit on success, autorollback on exception.
  • Every self.database.X call and every Result construction belongs inside this block.

Pattern 3 — read then write (single transaction)

When a write needs data that was just read, put both in one async with self.database: block:

python
async def __call__(self, request: ConfirmRegisterRequest) -> TokensExpire:
    cached_data = await self.cache.get(key)
    if not cached_data:
        raise NotFoundError("Code has expired or invalid")

    async with self.database:
        role = (await self.database.role.select(name="User")).result()       # read
        user = (
            await self.database.user.create(                                   # write
                login=cached_user.login,
                password=self.hasher.hash_password(cached_user.password),
                role_uuid=role.uuid,
            )
        ).result()

    return await self.services.auth.login(cached_user.fingerprint, user.uuid)

One session. One transaction. Both operations atomic.

Anti-patterns (hook will not catch these — you must self-check)

❌ Anti-patternWhy it breaks✅ Fix
Opening the session twice: one async with self.database.manager.session: for the read, then a second async with self.database: for the writeThe session is single-use — second async with fails on a closed sessionCombine both into a single async with self.database: block
Nesting async with self.database: and async with self.database.manager.session:Double-entering the same sessionPick one — for writes, async with self.database: only
WidgetResult.model_validate(widget) where widget is an ORM rowPydantic's from_attributes=True does getattr(widget, "owner"), which triggers SQLAlchemy lazy loading → MissingGreenlet on an async sessionUse WidgetResult(**widget.as_dict()) — Base.as_dict() iterates __dict__, which contains only already-loaded attributes; missing relationships fall back to the Pydantic field default (None)
Mutating inside async with self.database.manager.session:No session.begin() → no commit → silent data lossSwitch to async with self.database:
Calling self.database.widget.create(...) outside any contextSession state undefinedWrap in the appropriate context
Expecting a relationship to appear in the Result when the caller did not pass it in loadsBase.as_dict() skips unloaded relations, so the field ends up None — if your use case depends on the relation, the response is silently wrongEither forward *request.loads to the repo call and document that the relation is optional in the Result, or add the relation to loads unconditionally inside the use case (rare — usually it's the caller's choice)

Rule of thumb: when in doubt, use async with self.database:. It works for both reads and writes — worst case you pay for a transaction you didn't need. The other direction (using manager.session for a write) silently loses data.

Mapping ORM → Result — use Base.as_dict()

Every ORM model inherits Base.as_dict() (defined in src/database/psql/models/base/core.py). This is the only way you should build a Result from an ORM row.

python
# Single
return WidgetResult(**widget.as_dict())

# List (inside select_many use cases)
data=[WidgetResult(**w.as_dict()) for w in widgets]

Why this works and model_validate doesn't

Base.as_dict() iterates self.__dict__ directly. SQLAlchemy puts only loaded attributes in __dict__:

  • Columns (uuid, name, owner_uuid, created_at, …) are populated at SELECT time → always present.
  • Relationships (owner, tags, …) are populated only if the caller eager-loaded them via loads=.... Otherwise they're not in __dict__ at all — they live on the class descriptor and would be fetched via lazy-load on getattr.

So widget.as_dict() never triggers a lazy load — it doesn't do getattr, it reads __dict__ directly. Missing relationship → missing dict key → Pydantic falls back to the field default (owner: OwnerResult | None = None).

By contrast, WidgetResult.model_validate(widget) uses Pydantic's from_attributes=True mode, which walks the declared fields and calls getattr(widget, "owner") — that triggers SQLAlchemy's lazy load, which inside an async session crashes with MissingGreenlet. Never do this for ORM rows. (It's still fine for dicts, Results, and other non-ORM objects.)

The load contract

The caller decides which relations appear in the response via loads=...:

  • loads=() (default) → only columns; relationships come back as None and the HTTP serializer (exclude_none=True in presentation/http/common/serializers/default.py) drops them from the JSON entirely.
  • loads=("owner",) → as_dict() finds owner in __dict__ as a nested Base, recursively converts it to a dict, and Pydantic coerces the dict into OwnerResult.

Don't defensively check inspect(orm).unloaded in the use case. The load contract is the opt-in — honour it and let as_dict() do its thing.

Extra ORM columns vs declared Result fields

Base.as_dict() returns every column in __dict__, including ones the Result doesn't declare (password, created_at, updated_at, role_uuid, …). Pydantic v2 defaults to extra="ignore", so these silently drop during WidgetResult(**widget.as_dict()). No config needed.

List (select_many) use cases — special shape

python
from src.application.common.pagination import OffsetPagination
from src.application.v1.results import OffsetResult, WidgetResult


class SelectManyWidgetRequest(Request):
    loads: tuple[WidgetLoads, ...] = ()
    name: str | None = None
    pagination: OffsetPagination = OffsetPagination()


@dataclass(slots=True)
class SelectManyWidgetUseCase(
    UseCase[SelectManyWidgetRequest, OffsetResult[WidgetResult]]
):
    database: DBGateway

    async def __call__(
        self, request: SelectManyWidgetRequest
    ) -> OffsetResult[WidgetResult]:
        async with self.database.manager.session:
            total, widgets = (
                await self.database.widget.select_many(
                    *request.loads,
                    name=request.name,
                    **request.pagination.model_dump(),
                )
            ).result()

            return OffsetResult[WidgetResult](
                data=[WidgetResult(**w.as_dict()) for w in widgets],
                offset=request.pagination.offset,
                limit=request.pagination.limit,
                total=total,
            )
  • pagination: OffsetPagination = OffsetPagination() — the application version (src/application/common/pagination.py), which allows limit: int | None = None (unbounded for internal callers).
  • **request.pagination.model_dump() spreads order_by, offset, limit into the repo call.
  • WidgetResult(**w.as_dict()) — see "Mapping ORM → Result" above.
  • Returns OffsetResult[WidgetResult] — the generic envelope with .map() so the endpoint can retarget item types.

Anti-patterns (hard no)

  • ❌ Importing anything from src.infrastructure.*, src.presentation.*, src.consumers.*, src.tasks.*. The layer hook will block this, but be aware anyway.
  • ❌ Importing a Contract class from presentation/ and returning it. Return Result types only.
  • ❌ Calling session.execute directly — go through the repository.
  • ❌ Writing two separate async with blocks on self.database in one use case.
  • ❌ Adding business logic to a Request class. Request is a frozen Pydantic model — validation only.
  • ❌ Registering a use case in setup_use_cases() but forgetting to re-export it from usecases/<domain>/__init__.py.
  • ❌ Using @dataclass without slots=True. The base class auto-applies it, but being explicit keeps mypy happy.

الأسئلة الشائعة

ماذا تفعل مهارة الذكاء الاصطناعي Usecase؟

Use when creating or editing a use case under src/application/v1/usecases/. Enforces the RequestBus + UseCase pattern (Request + UseCase in one file, @dataclass(slots=True), returns Result type) and the DBGateway transaction rules — never open two sessions in one use case, never touch self.database outside a context manager, never lazy-load relationships outside the session.

لماذا تستخدم Usecase على TypingMind؟

لأنك تثبّتها مرة واحدة وتستخدمها مع أي نموذج. مهارة Usecase عبارة عن نص Markdown وليست شيفرة خاصة بمزود بعينه، لذا تعمل المهارة نفسها مع GPT-5 أو Claude أو Gemini أو Grok أو نموذج محلي، ويمكنك تبديل النموذج أثناء المحادثة دون تعطّل المهارة. يعمل TypingMind بمفاتيح API الخاصة بك، فتدفع للمزودين مباشرةً بدلًا من اشتراك لكل مقعد، وتبقى مهاراتك ومحادثاتك في مساحة التخزين الخاصة بك.

كيف أثبّت Usecase في TypingMind؟

افتح الإضافات → المهارات → التثبيت من GitHub في TypingMind والصق https://github.com/ewtryocb/fastapi-claude-template/tree/master/.claude/skills/usecase. يقرأ TypingMind ملف SKILL.md الخاص بها ويثبّتها كمهارة يمكنك تفعيلها لكل محادثة على حدة.

ما نماذج الذكاء الاصطناعي التي يمكنها استخدام Usecase؟

أي نموذج تربطه في TypingMind. مهارات الذكاء الاصطناعي عبارة عن تعليمات Markdown وليست شيفرة خاصة بمزود بعينه، لذا يمكن لكل من GPT وClaude وGemini وGrok والنماذج المحلية تحميل هذه المهارة عندما يتوافق الطلب معها.

كم نموذج ذكاء اصطناعي يمكنني استخدامه مع Usecase؟

أي عدد تريده. ما دام النموذج يدعم المهارات، يمكنك استخدام Usecase معه، سواء كان GPT أو Claude أو Gemini أو Grok أو DeepSeek أو Mistral أو Llama أو غيرها، وكل ذلك على TypingMind باستخدام مفاتيح API الخاصة بك.

هل مهارة الذكاء الاصطناعي Usecase مجانية؟

نعم. نشرها ewtryocb على GitHub بموجب ترخيص MIT. لا تدفع إلا لمزود الذكاء الاصطناعي الخاص بك مقابل التوكنات التي تستخدمها.

ما هي مهارات الذكاء الاصطناعي؟

مهارة الذكاء الاصطناعي حزمة تعليمات قابلة لإعادة الاستخدام تعلّم نموذج الذكاء الاصطناعي كيفية تنفيذ مهمة محددة. وتتبع تنسيق Agent Skills المفتوح: ملف SKILL.md يتضمن اسمًا ووصفًا، إلى جانب أي نصوص برمجية أو قوالب أو ملفات مرجعية قد يحتاج إليها النموذج. لا يقرأ النموذج التعليمات إلا عندما يتوافق طلبك مع المهارة، لذا لا تترتب على المهارة المثبتة أي تكلفة حتى تُستخدم.

كيف تختلف مهارات الذكاء الاصطناعي عن الإضافات أو موصلات MCP؟

تمنح الإضافة أو موصل MCP النموذج أدوات جديدة يمكنه استدعاؤها، وهي شيفرة تُنفَّذ في مكان ما وتُرجع نتيجة. أما مهارة الذكاء الاصطناعي فتزوّد النموذج بالمعرفة ومنهجية العمل: كيفية التعامل مع مهمة، والخطوات التي ينبغي اتباعها، وما ينبغي أن تكون عليه المخرجات الجيدة. المهارات عبارة عن نصوص Markdown، لذا لا تحتاج إلى خادم أو مفتاح API أو بيئة تشغيل، وتعمل مع أي نموذج.

عرض الكل

أنشئ مساحة عملك للذكاء الاصطناعي الآن

اشترك في نشرتنا البريدية لتصلك أخبار الميزات الجديدة والهدايا القادمة 👇