Services are used to define the services for the controller
Services in Orionjs encapsulate business logic and provide a clean way to organize your application code. They follow the dependency injection pattern, making your code more modular and easier to test.
Services can be injected into other services or controllers. In v4, you must use the factory function pattern for dependency injection:
Copy
import {Service, Inject} from '@orion-js/services'import {UserService} from './UserService'@Service()export class AuthService { // Use factory function for dependency injection @Inject(() => UserService) private userService: UserService async login(email: string, password: string) { // Use injected service const user = await this.userService.findByEmail(email) // Implementation... return token }}
You can get service instances from anywhere in your application:
Copy
import {getInstance} from '@orion-js/services'import {UserService} from './UserService'// Get an instance of the serviceconst userService = getInstance(UserService)// Use the serviceawait userService.findById('123')
Services form the backbone of Orionjs applications, providing a structured way to organize business logic and maintain separation of concerns.