Junior — Middle
How is the context used in implementing business rules through UseCase?
sobes.tech AI
Answer from AI
In architecture using UseCase, the context serves to pass all necessary information that affects the execution of business rules. The context contains data, state, and parameters needed for the correct operation of a specific UseCase.
For example, in an iOS application, the context may include the current user, application settings, session state, and other parameters that influence business logic. The UseCase receives this context and makes decisions, performs checks, and changes state based on it.
Example in Swift:
struct UserContext {
let userId: String
let isPremiumUser: Bool
}
class PurchaseUseCase {
func execute(context: UserContext, itemId: String) {
if context.isPremiumUser {
// apply discount
} else {
// regular price
}
// further purchase logic
}
}
Thus, the context provides flexibility and reusability of business rules, allowing the UseCase to adapt to different conditions and data.