Skip to content

Latest commit

 

History

History
77 lines (55 loc) · 1.74 KB

File metadata and controls

77 lines (55 loc) · 1.74 KB

Design Patterns

Common design patterns used in the Accellens codebase.

Repository Pattern

Used for data access logic to decouple the business layer from the data source (database).

Location: apps/services/crud/

Usage: All database interactions should go through a Repository (CRUD class).

# Interface/Base
class CRUDBase(Generic[ModelType]):
    async def get(self, db: AsyncSession, id: Any) -> ModelType | None: ...

# Implementation
class UserCRUD(CRUDBase[User]):
    async def get_by_email(self, db: AsyncSession, email: str) -> User | None: ...

Factory Pattern

Used to create objects without specifying the exact class of object that will be created.

Location: apps/frontend/src/lib/api/factory.ts

Usage: Creating consistent API clients.

export const createCrudApi = <TCreate, TUpdate, TEntity>(config: CrudApiConfig) => {
    return {
        list: () => ...,
        get: () => ...,
        create: () => ...,
    };
};

Dependency Injection

Used to provide dependencies to classes/functions rather than hardcoding them.

Usage (Backend): FastAPI Depends.

async def get_current_user(token: str = Depends(oauth2_scheme), db = Depends(get_db)):
    ...

Usage (Frontend): React Context and Custom Hooks.

const { createProject } = useProjects(); // Hook injects the API client logic

Observer Pattern

Used to notify multiple objects about any events that happen to the object they're observing.

Usage: Event handling in frontend (e.g., EventSource for scan updates).

// useScanEvents.ts
useEffect(() => {
  const source = new EventSource(url);
  source.onmessage = (event) => {
    // Observers react to event
  };
}, []);