|
| 1 | +--- |
| 2 | +id: api-reference |
| 3 | +title: API Reference |
| 4 | +sidebar_position: 1 |
| 5 | +--- |
| 6 | + |
| 7 | +import ApiReference from '@site/src/components/ApiReference'; |
| 8 | + |
| 9 | +# Structus API Reference |
| 10 | + |
| 11 | +Complete API documentation for the Structus library. |
| 12 | + |
| 13 | +<ApiReference |
| 14 | + title="Core Aggregates" |
| 15 | + description="Base interfaces and classes for building domain models" |
| 16 | + endpoints={[ |
| 17 | + { |
| 18 | + method: "interface", |
| 19 | + path: "AggregateRoot<ID>", |
| 20 | + description: "Base interface for aggregate root entities. All domain model aggregates must implement this interface.", |
| 21 | + parameters: [ |
| 22 | + { |
| 23 | + name: "ID", |
| 24 | + type: "Serializable", |
| 25 | + description: "The type of the aggregate's identifier", |
| 26 | + required: true, |
| 27 | + } |
| 28 | + ], |
| 29 | + returns: { |
| 30 | + type: "Entity with event sourcing capabilities", |
| 31 | + description: "Provides methods for managing domain events and state changes" |
| 32 | + }, |
| 33 | + example: `data class OrderAggregate( |
| 34 | + override val id: OrderId, |
| 35 | + val items: List<OrderItem> = emptyList(), |
| 36 | + val status: OrderStatus = OrderStatus.PENDING |
| 37 | +) : AggregateRoot<OrderId> { |
| 38 | + override fun getUncommittedEvents(): List<DomainEvent> { |
| 39 | + return emptyList() |
| 40 | + } |
| 41 | +}` |
| 42 | + }, |
| 43 | + { |
| 44 | + method: "interface", |
| 45 | + path: "Entity<ID>", |
| 46 | + description: "Base interface for entities within an aggregate. Entities have identity and lifecycle within aggregates.", |
| 47 | + parameters: [ |
| 48 | + { |
| 49 | + name: "ID", |
| 50 | + type: "Serializable", |
| 51 | + description: "The type of the entity's identifier", |
| 52 | + required: true, |
| 53 | + } |
| 54 | + ], |
| 55 | + returns: { |
| 56 | + type: "Entity with value object support", |
| 57 | + description: "Supports composition with value objects for rich domain models" |
| 58 | + } |
| 59 | + }, |
| 60 | + { |
| 61 | + method: "class", |
| 62 | + path: "ValueObject", |
| 63 | + description: "Abstract base class for value objects. Value objects have no identity and are immutable.", |
| 64 | + returns: { |
| 65 | + type: "Immutable domain concept", |
| 66 | + description: "Used for modeling concepts like Money, Email, or ProductCode" |
| 67 | + }, |
| 68 | + example: `data class Money( |
| 69 | + val amount: BigDecimal, |
| 70 | + val currency: Currency |
| 71 | +) : ValueObject() { |
| 72 | + init { |
| 73 | + require(amount >= BigDecimal.ZERO) { "Amount must be non-negative" } |
| 74 | + } |
| 75 | +}` |
| 76 | + } |
| 77 | + ]} |
| 78 | +/> |
| 79 | + |
| 80 | +<ApiReference |
| 81 | + title="Repository Pattern" |
| 82 | + description="Interfaces for persisting and retrieving aggregates" |
| 83 | + endpoints={[ |
| 84 | + { |
| 85 | + method: "interface", |
| 86 | + path: "Repository<T, ID>", |
| 87 | + description: "Base repository interface for storing and retrieving aggregates of type T", |
| 88 | + parameters: [ |
| 89 | + { |
| 90 | + name: "T", |
| 91 | + type: "AggregateRoot<ID>", |
| 92 | + description: "The aggregate root type", |
| 93 | + required: true, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "ID", |
| 97 | + type: "Serializable", |
| 98 | + description: "The aggregate's identifier type", |
| 99 | + required: true, |
| 100 | + } |
| 101 | + ], |
| 102 | + returns: { |
| 103 | + type: "Query and persistence operations", |
| 104 | + description: "Provides find, save, and delete operations" |
| 105 | + }, |
| 106 | + example: `interface OrderRepository : Repository<OrderAggregate, OrderId> { |
| 107 | + suspend fun findByCustomerId(customerId: CustomerId): List<OrderAggregate> |
| 108 | + suspend fun findRecentOrders(limit: Int): List<OrderAggregate> |
| 109 | +}` |
| 110 | + } |
| 111 | + ]} |
| 112 | +/> |
| 113 | + |
| 114 | +<ApiReference |
| 115 | + title="Command Handling" |
| 116 | + description="Interfaces for handling commands and business operations" |
| 117 | + endpoints={[ |
| 118 | + { |
| 119 | + method: "interface", |
| 120 | + path: "CommandHandler<T, R>", |
| 121 | + description: "Handles a command of type T and returns a result of type R", |
| 122 | + parameters: [ |
| 123 | + { |
| 124 | + name: "T", |
| 125 | + type: "Command", |
| 126 | + description: "The command to handle", |
| 127 | + required: true, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "R", |
| 131 | + type: "Any", |
| 132 | + description: "The return type of the command handler", |
| 133 | + required: true, |
| 134 | + } |
| 135 | + ], |
| 136 | + returns: { |
| 137 | + type: "Result<R>", |
| 138 | + description: "Either a successful result or a domain error" |
| 139 | + }, |
| 140 | + example: `class CreateOrderCommandHandler( |
| 141 | + private val orderRepository: OrderRepository |
| 142 | +) : CommandHandler<CreateOrderCommand, OrderId> { |
| 143 | + override suspend fun handle(command: CreateOrderCommand): OrderId { |
| 144 | + val order = OrderAggregate.create( |
| 145 | + customerId = command.customerId, |
| 146 | + items = command.items |
| 147 | + ) |
| 148 | + orderRepository.save(order) |
| 149 | + return order.id |
| 150 | + } |
| 151 | +}` |
| 152 | + } |
| 153 | + ]} |
| 154 | +/> |
| 155 | + |
| 156 | +## Best Practices |
| 157 | + |
| 158 | +- **Aggregate Design**: Keep aggregates small and focused on a single responsibility |
| 159 | +- **Event Sourcing**: Use domain events to capture state changes |
| 160 | +- **Repository Contract**: Define repositories per aggregate, not per entity |
| 161 | +- **Command Validation**: Validate commands at the handler level |
| 162 | +- **Error Handling**: Use result types or exceptions for domain errors |
| 163 | + |
| 164 | +## See Also |
| 165 | + |
| 166 | +- [Architecture Overview](/docs/architecture/overview) |
| 167 | +- [Getting Started](/docs/getting-started/quick-start) |
| 168 | +- [Advanced Patterns](/docs/advanced/cqrs-implementation) |
| 169 | + |
0 commit comments