Skip to content

Services

Maicon edited this page Sep 7, 2023 · 2 revisions

Establish services to encapsulate your business logic within the domain layer, making it accessible in the application layer:

class TesteService extends Services {
	#[Autowired(class: TesteService::class)]
	public TesteService $testeService;
	
	public function execute(){}
}

Using Services in PotatoService

In the PotatoService framework, services play a pivotal role in maintaining a clean architecture. They encapsulate your business logic within the domain layer, ensuring that your application remains modular, maintainable, and scalable. By delegating the core logic to services, you can easily access and utilize this logic in the application layer, keeping your controllers lightweight and focused.

Defining a Service:

Services should extend the base Services class. This not only standardizes the structure but also provides a suite of utilities and features that are common to all services.

namespace your\namespace;

class TesteService extends Services {
    // Your service methods and attributes
}

Dependency Injection with Autowired:

PotatoService offers a powerful dependency injection mechanism through the Autowired attribute. This attribute allows services to automatically instantiate and inject dependencies, promoting the principle of inversion of control.

#[Autowired(class: AnotherService::class)] public AnotherService $anotherService;

In the example above, the AnotherService dependency is automatically instantiated and injected into the TesteService class. This eliminates the need for manual instantiation and promotes a decoupled architecture.

Working with Attributes:

Attributes in PotatoService offer metadata-driven enhancements to your methods or properties. They can define specific behaviors, validations, or preprocessing tasks.

For instance, if you have an attribute that needs to preprocess data before the service method is called, you can define and apply it as follows:

#[YourAttribute]
public function yourServiceMethod($data) {
    // Your core method logic
}

The attribute's logic will be executed before the main method's logic when using the doFilter function to call this method.

Best Practices:

  1. Single Responsibility: Ensure each service adheres to the Single Responsibility Principle. A service should handle a specific domain or functionality of your application.

  2. Utilize Attributes: Use attributes to minimize repetitive code and add behaviors to methods or properties without altering their core logic.

  3. Testability: Design your services with testing in mind. By keeping them decoupled and using dependency injection, you can easily write unit tests for your services.

In Summary:

Services in PotatoService are instrumental in ensuring a clean and organized architecture. By encapsulating business logic, providing dependency injection, and offering attribute-driven behaviors, they promote a scalable and maintainable codebase. Always keep in mind the core principles of OOP and design patterns when crafting your services for optimal results.