Sub topic 1
Why Group Code by Feature, Not by File Type
Prerequisites
You should know TypeScript, basic Angular components and dependency injection, and the Angular Router. You should also be comfortable using the Angular CLI and npm.
Organize Around User Capabilities
A file-type structure groups all components, services, and models separately:
components/
services/
models/This looks tidy at first. As the application grows, one feature may spread across many folders. Finding the code for orders requires repeated searches. Removing or changing orders also risks touching unrelated files.
A feature structure keeps code that changes together together:
orders/
data-access/
feature/
ui/
util/
orders.routes.ts
index.ts
customers/
data-access/
feature/
ui/An orders feature contains the screens, data access, reusable interface pieces, and routes for orders. This gives the feature a clearer boundary and makes ownership easier to understand.
Standalone Components as Building Blocks
A standalone component declares its own imports instead of depending on an Angular module. This makes the component easier to move, test, and compose.
Use standalone components for feature pages and reusable UI pieces. A page component belongs inside a feature. A button or table used by several features belongs in a shared UI area.
The component should depend on the smallest useful boundary. For example, an order page can import an order table component without knowing how the table loads data.
Core and Shared Code
Keep application-wide infrastructure in core. Examples include authentication services, HTTP setup, route guards, and global error handling. Core code usually has one application-wide instance.
Keep broadly reusable, domain-neutral pieces in shared. Examples include a button component, a date formatter, or a validation utility. Shared code should not import a specific feature.
Use this test: if code exists because the business has orders, keep it in orders. If it supports the whole application without knowing a business domain, consider core or shared.
> callout note > Do not put code in shared merely because several files use it. Shared code creates dependencies across features, so keep its API small and stable. > ```
Exercise
Exercise
Where should an order-specific date formatter used only by the orders screen live?
Show answer
Inside the orders feature, because its purpose belongs to that business domain.