API REST con Spring Boot | 9 | Repositorios y Servicios Genéricos
Introduction and Generic Repository Implementation
In this section, the speaker introduces the topic of implementing generics in a project and begins by creating generic repositories.
Creating Generic Repositories
- Creating a new interface named
BaseRepositoryto serve as the foundation for all entity repositories. Utilizing the@NoRepositoryBeanannotation to prevent instances of this repository.
- : The
BaseRepositoryinterface will receive two types: entity type extendingBaseand an additional type called "air" representing the entity's air type.
- Modifying the existing personal repository to extend from
BaseRepository.
- : Changing the extension from
JpaRepositorytoBaseRepository.
- Adding an author repository by annotating it with
@Repository, extending it fromBaseRepository, and specifying the type as "author".
- : Extending the author repository from
BaseRepository<Author>.
Service Modification and Abstract Implementation
This part focuses on enhancing services by making them abstract and implementing generic methods.
Enhancing Services
- Improving base services by extending them from a base class, importing necessary dependencies, and ensuring compatibility with different data types.
- : Updating service structures to extend from base classes for flexibility in data types used across projects.
- Adapting service implementations to be abstract, allowing inheritance of methods without manual implementation.
- : Implementing abstract service classes using shortcuts provided by IDE tools.
Generic Service Implementation
Discusses creating a generic service implementation that can be inherited by specific services for method reuse.
Generic Service Implementation
- Generating a generic service implementation that receives types 'E' and 'A', implements interfaces, and initializes a base repository instance.
- : Describing the structure of the generic service implementation class with type parameters 'E' and 'A'.
- Initializing a protected repository within the service constructor for accessing specific model repositories efficiently.
Podemos Acceder a los Métodos
In this section, the speaker discusses accessing methods within a repository and introduces the concept of creating a repository for people to access all included methods.
Accessing Methods in Repositories
- Methods cannot be accessed from a repository directly when making queries within it.
- To access methods from a base repository, an "auto wire" of a person's repository is created.
- This allows access to all methods included in the person's repository.
Creating Services
The speaker explains the process of creating services by starting with an author service class within the package.
Creating Author Service Class
- Create a new class named "AuthorService" within the package.
- Add the annotation "@Service" to indicate it as a service and extend it from "BaseServiceImplementation" passing author types.
Implementing Generic Service Structure
The focus is on completing the generic structure of services, including interfaces and abstract implementations for reusability across projects.
Completing Service Structure
- Implement constructors as required and instantiate with "auto wire."
- The generic service structure includes an interface, an abstract implementation, and specific services for models needed in projects.
Utilizing Interfaces for Specific Services
Discusses utilizing interfaces for specific services to maintain clarity on implemented methods for each service.
Interface for Person Service
- Create an interface for "PersonService" declaring specific methods for person-related services.
- Implement this interface to have clear visibility on which methods each service implements.
Refactoring and Renaming Classes
Focuses on renaming classes to enhance organization and clarity in understanding method implementations per service.
Refactoring Classes
- Rename classes using refactoring tools like renaming "PersonServicesImplementation" to improve naming conventions.
Creating Additional Interfaces
Introduces creating additional interfaces such as "AuthorService" extending from BaseService with author types.
Creating Author Interface
- Create an interface named "AuthorService," extending from BaseService with author types.
Completing Module Services Implementation
Concludes by implementing interfaces like AuthorService and PersonService, finalizing module services until further expansion requires additional services.
Finalizing Module Services