Common design patterns used in the Accellens codebase.
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: ...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: () => ...,
};
};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 logicUsed 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
};
}, []);