|
| 1 | +# kediatR Migration Guide |
| 2 | + |
| 3 | +## Breaking Changes: Command Type Hierarchy Unification |
| 4 | + |
| 5 | +This migration guide covers the breaking changes introduced in the command type hierarchy unification. The main change is the removal of `CommandWithResult` and `CommandWithResultHandler` interfaces in favor of a unified `Command<TResult>` interface. |
| 6 | + |
| 7 | +### Summary of Changes |
| 8 | + |
| 9 | +- **Removed**: `CommandWithResult` interface |
| 10 | +- **Removed**: `CommandWithResultHandler` interface |
| 11 | +- **Modified**: `Command` interface now accepts a generic type parameter `TResult` |
| 12 | +- **Added**: `Command.Unit` nested interface for commands that don't return results |
| 13 | +- **Modified**: `CommandHandler` interface now handles both unit and result-returning commands |
| 14 | +- **Added**: `CommandHandler.Unit` nested interface for unit command handlers |
| 15 | + |
| 16 | +### Before (Old API) |
| 17 | + |
| 18 | +```kotlin |
| 19 | +// Unit commands (no result) |
| 20 | +interface Command { |
| 21 | + val type: Class<out Command> get() = this::class.java |
| 22 | +} |
| 23 | + |
| 24 | +interface CommandHandler<TCommand : Command> { |
| 25 | + suspend fun handle(command: TCommand) |
| 26 | +} |
| 27 | + |
| 28 | +// Commands with results |
| 29 | +interface CommandWithResult<TResult> { |
| 30 | + val type: Class<out CommandWithResult<TResult>> get() = this::class.java |
| 31 | +} |
| 32 | + |
| 33 | +interface CommandWithResultHandler<TCommand : CommandWithResult<TResult>, TResult> { |
| 34 | + suspend fun handle(command: TCommand): TResult |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +### After (New API) |
| 39 | + |
| 40 | +```kotlin |
| 41 | +// Unified command interface |
| 42 | +interface Command<TResult> { |
| 43 | + val type: Class<out Command<TResult>> get() = this::class.java |
| 44 | + |
| 45 | + // Nested interface for unit commands |
| 46 | + interface Unit : Command<kotlin.Unit> |
| 47 | +} |
| 48 | + |
| 49 | +// Unified command handler interface |
| 50 | +interface CommandHandler<TCommand : Command<TResult>, TResult> { |
| 51 | + suspend fun handle(command: TCommand): TResult |
| 52 | + |
| 53 | + // Nested interface for unit command handlers |
| 54 | + interface Unit<TCommand : Command.Unit> : CommandHandler<TCommand, kotlin.Unit> |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Migration Steps |
| 59 | + |
| 60 | +### 0. Optional: Use Type Aliases for Gradual Migration |
| 61 | + |
| 62 | +To ease the migration process, you can temporarily add type aliases to your codebase. This allows you to migrate incrementally without breaking existing code: |
| 63 | + |
| 64 | +```kotlin |
| 65 | +// Add these type aliases to ease migration |
| 66 | +typealias CommandWithResult<T> = Command<T> |
| 67 | +typealias CommandWithResultHandler<TCommand, TResult> = CommandHandler<TCommand, TResult> |
| 68 | +``` |
| 69 | + |
| 70 | +**Benefits:** |
| 71 | +- Allows gradual migration without breaking existing code |
| 72 | +- Helps during large codebase migrations |
| 73 | +- Can be removed once migration is complete |
| 74 | + |
| 75 | +**Usage:** |
| 76 | +1. Add the type aliases to a common file (e.g., `TypeAliases.kt`) |
| 77 | +2. Import them where needed |
| 78 | +3. Gradually replace usage with the new interfaces |
| 79 | +4. Remove the type aliases once migration is complete |
| 80 | + |
| 81 | +### 1. Update Unit Commands |
| 82 | + |
| 83 | +**Before:** |
| 84 | +```kotlin |
| 85 | +class CreateUserCommand : Command { |
| 86 | + // command properties |
| 87 | +} |
| 88 | + |
| 89 | +class CreateUserCommandHandler : CommandHandler<CreateUserCommand> { |
| 90 | + override suspend fun handle(command: CreateUserCommand) { |
| 91 | + // handle command |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +**After:** |
| 97 | +```kotlin |
| 98 | +class CreateUserCommand : Command.Unit { |
| 99 | + // command properties |
| 100 | +} |
| 101 | + |
| 102 | +class CreateUserCommandHandler : CommandHandler.Unit<CreateUserCommand> { |
| 103 | + override suspend fun handle(command: CreateUserCommand) { |
| 104 | + // handle command |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### 2. Update Commands with Results |
| 110 | + |
| 111 | +**Before:** |
| 112 | +```kotlin |
| 113 | +class GetUserCommand(val userId: String) : CommandWithResult<User> { |
| 114 | + // command properties |
| 115 | +} |
| 116 | + |
| 117 | +class GetUserCommandHandler : CommandWithResultHandler<GetUserCommand, User> { |
| 118 | + override suspend fun handle(command: GetUserCommand): User { |
| 119 | + // handle command and return result |
| 120 | + return userRepository.findById(command.userId) |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +**After:** |
| 126 | +```kotlin |
| 127 | +class GetUserCommand(val userId: String) : Command<User> { |
| 128 | + // command properties |
| 129 | +} |
| 130 | + |
| 131 | +class GetUserCommandHandler : CommandHandler<GetUserCommand, User> { |
| 132 | + override suspend fun handle(command: GetUserCommand): User { |
| 133 | + // handle command and return result |
| 134 | + return userRepository.findById(command.userId) |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +### 3. Update Mediator Usage |
| 140 | + |
| 141 | +The mediator usage remains the same - no changes needed: |
| 142 | + |
| 143 | +```kotlin |
| 144 | +// Unit commands |
| 145 | +mediator.send(CreateUserCommand()) |
| 146 | + |
| 147 | +// Commands with results |
| 148 | +val user = mediator.send(GetUserCommand("123")) |
| 149 | +``` |
| 150 | + |
| 151 | +### 4. Update Dependency Injection Registration |
| 152 | + |
| 153 | +The handler registration remains the same since the framework automatically detects the correct handler types: |
| 154 | + |
| 155 | +#### Spring Boot |
| 156 | +```kotlin |
| 157 | +@Component |
| 158 | +class CreateUserCommandHandler : CommandHandler.Unit<CreateUserCommand> { |
| 159 | + // implementation |
| 160 | +} |
| 161 | + |
| 162 | +@Component |
| 163 | +class GetUserCommandHandler : CommandHandler<GetUserCommand, User> { |
| 164 | + // implementation |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +#### Koin |
| 169 | +```kotlin |
| 170 | +module { |
| 171 | + single { CreateUserCommandHandler() } bind CommandHandler::class |
| 172 | + single { GetUserCommandHandler() } bind CommandHandler::class |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +#### Manual Registration |
| 177 | +```kotlin |
| 178 | +val mediator = MappingDependencyProvider.createMediator( |
| 179 | + handlers = listOf( |
| 180 | + CreateUserCommandHandler(), |
| 181 | + GetUserCommandHandler() |
| 182 | + ) |
| 183 | +) |
| 184 | +``` |
| 185 | + |
| 186 | +## Advanced Migration Scenarios |
| 187 | + |
| 188 | +### 1. Parameterized Commands |
| 189 | + |
| 190 | +**Before:** |
| 191 | +```kotlin |
| 192 | +class ParameterizedCommand<T>(val param: T) : Command |
| 193 | + |
| 194 | +class ParameterizedCommandHandler<T> : CommandHandler<ParameterizedCommand<T>> { |
| 195 | + override suspend fun handle(command: ParameterizedCommand<T>) { |
| 196 | + // handle |
| 197 | + } |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +**After:** |
| 202 | +```kotlin |
| 203 | +class ParameterizedCommand<T>(val param: T) : Command.Unit |
| 204 | + |
| 205 | +class ParameterizedCommandHandler<T> : CommandHandler.Unit<ParameterizedCommand<T>> { |
| 206 | + override suspend fun handle(command: ParameterizedCommand<T>) { |
| 207 | + // handle |
| 208 | + } |
| 209 | +} |
| 210 | +``` |
| 211 | + |
| 212 | +### 2. Parameterized Commands with Results |
| 213 | + |
| 214 | +**Before:** |
| 215 | +```kotlin |
| 216 | +class ParameterizedCommandWithResult<TParam, TReturn>( |
| 217 | + val param: TParam, |
| 218 | + val retFn: suspend (TParam) -> TReturn |
| 219 | +) : CommandWithResult<TReturn> |
| 220 | + |
| 221 | +class ParameterizedCommandWithResultHandler<TParam, TReturn> : |
| 222 | + CommandWithResultHandler<ParameterizedCommandWithResult<TParam, TReturn>, TReturn> { |
| 223 | + override suspend fun handle(command: ParameterizedCommandWithResult<TParam, TReturn>): TReturn { |
| 224 | + return command.retFn(command.param) |
| 225 | + } |
| 226 | +} |
| 227 | +``` |
| 228 | + |
| 229 | +**After:** |
| 230 | +```kotlin |
| 231 | +class ParameterizedCommandWithResult<TParam, TReturn>( |
| 232 | + val param: TParam, |
| 233 | + val retFn: suspend (TParam) -> TReturn |
| 234 | +) : Command<TReturn> |
| 235 | + |
| 236 | +class ParameterizedCommandWithResultHandler<TParam, TReturn> : |
| 237 | + CommandHandler<ParameterizedCommandWithResult<TParam, TReturn>, TReturn> { |
| 238 | + override suspend fun handle(command: ParameterizedCommandWithResult<TParam, TReturn>): TReturn { |
| 239 | + return command.retFn(command.param) |
| 240 | + } |
| 241 | +} |
| 242 | +``` |
| 243 | + |
| 244 | +### 3. Inheritance Scenarios |
| 245 | + |
| 246 | +**Before:** |
| 247 | +```kotlin |
| 248 | +sealed class BaseCommand : Command { |
| 249 | + abstract val id: String |
| 250 | +} |
| 251 | + |
| 252 | +class SpecificCommand(override val id: String) : BaseCommand() |
| 253 | + |
| 254 | +class BaseCommandHandler : CommandHandler<BaseCommand> { |
| 255 | + override suspend fun handle(command: BaseCommand) { |
| 256 | + // handle |
| 257 | + } |
| 258 | +} |
| 259 | +``` |
| 260 | + |
| 261 | +**After:** |
| 262 | +```kotlin |
| 263 | +sealed class BaseCommand : Command.Unit { |
| 264 | + abstract val id: String |
| 265 | +} |
| 266 | + |
| 267 | +class SpecificCommand(override val id: String) : BaseCommand() |
| 268 | + |
| 269 | +class BaseCommandHandler : CommandHandler.Unit<BaseCommand> { |
| 270 | + override suspend fun handle(command: BaseCommand) { |
| 271 | + // handle |
| 272 | + } |
| 273 | +} |
| 274 | +``` |
| 275 | + |
| 276 | +## Benefits of the New API |
| 277 | + |
| 278 | +1. **Unified Interface**: Single `Command<TResult>` interface for all command types |
| 279 | +2. **Type Safety**: Better compile-time type checking with generic result types |
| 280 | +3. **Cleaner API**: Fewer interfaces to understand and implement |
| 281 | +4. **Consistency**: Aligns with `Query<TResult>` pattern already used in the library |
| 282 | +5. **Backward Compatibility**: Pipeline behaviors and mediator usage remain unchanged |
| 283 | + |
| 284 | +## Checklist for Migration |
| 285 | + |
| 286 | +- [ ] Replace `Command` implementations with `Command.Unit` |
| 287 | +- [ ] Replace `CommandHandler<TCommand>` with `CommandHandler.Unit<TCommand>` |
| 288 | +- [ ] Replace `CommandWithResult<TResult>` with `Command<TResult>` |
| 289 | +- [ ] Replace `CommandWithResultHandler<TCommand, TResult>` with `CommandHandler<TCommand, TResult>` |
| 290 | +- [ ] Update import statements to remove references to deleted interfaces |
| 291 | +- [ ] Test all command handlers to ensure they work correctly |
| 292 | +- [ ] Update any custom extensions or utilities that referenced the old interfaces |
| 293 | + |
| 294 | +## Troubleshooting |
| 295 | + |
| 296 | +### Common Compilation Errors |
| 297 | + |
| 298 | +1. **"Unresolved reference: CommandWithResult"** |
| 299 | + - Replace with `Command<TResult>` |
| 300 | + |
| 301 | +2. **"Unresolved reference: CommandWithResultHandler"** |
| 302 | + - Replace with `CommandHandler<TCommand, TResult>` |
| 303 | + |
| 304 | +3. **"Type mismatch" errors on unit commands** |
| 305 | + - Ensure unit commands implement `Command.Unit` |
| 306 | + - Ensure unit handlers implement `CommandHandler.Unit<TCommand>` |
| 307 | + |
| 308 | +### Runtime Issues |
| 309 | + |
| 310 | +1. **HandlerNotFoundException** |
| 311 | + - Ensure handlers are properly registered with dependency injection |
| 312 | + - Check that command and handler types match exactly |
| 313 | + |
| 314 | +2. **ClassCastException** |
| 315 | + - Verify generic type parameters are correctly specified |
| 316 | + - Ensure handler return types match command result types |
| 317 | + |
| 318 | +## Support |
| 319 | + |
| 320 | +If you encounter issues during migration, please: |
| 321 | + |
| 322 | +1. Check this migration guide thoroughly |
| 323 | +2. Review the test examples in the `testFixtures` directory |
| 324 | +3. Create an issue in the GitHub repository with: |
| 325 | + - Your current code |
| 326 | + - The error message |
| 327 | + - Expected behavior |
0 commit comments