Query and View

The Evento Framework empowers you to build robust and scalable applications with its well-defined approach to handling queries. This section delves into the core concepts of Query Payloads and Messages, providing the foundation for effective data retrieval within your Evento applications.

By leveraging these concepts, you can design a clear separation between your queries and their corresponding response structures. This improves code readability, and maintainability, and promotes a consistent communication pattern within your microservices architecture.

Source Code Breakdown

The following code snippets showcase the key Evento classes involved in query handling:

1. Query Class (Query.java):

/**
 * The Query class is an abstract class that represents a query object. It extends the TrackablePayload class.
 * It is generic, with a type parameter T that must extend the QueryResponse class.
 *
 * @param <T> The type of the response object that the Query returns.
 *
 * @see TrackablePayload
 * @see QueryResponse
 */
public abstract class Query<T extends QueryResponse<?>> extends TrackablePayload {

	/**
	 * Returns the response type of the Query.
	 *
	 * @return The Class object representing the response type.
	 */
	@SuppressWarnings("unchecked")
	public Class<T> getResponseType() {
		return (Class<T>) ((ParameterizedType) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0]).getRawType();
	}
}

The Query class establishes the foundation for building queries. It enforces the TrackablePayload class, indicating that queries carry data. The generic type parameter T allows you to specify the expected response type (QueryResponse). The getResponseType() method leverages generics to determine the Class object representing the expected response data structure.

2. QueryMessage (QueryMessage.java):

The QueryMessage class acts as a container for Query objects. It inherits from the Message class, providing functionalities for message routing and handling. Additionally, it retrieves the query name from the underlying payload (if it has a name).

3. QueryResponse (QueryResponse.java):

The QueryResponse class serves as an abstract base class for all query response objects. It enforces data serialization by implementing Serializable. The generic type parameter T restricts the response data type to extend the View interface (discussed later). Concrete implementations like Single<T> and Multiple<T> cater to different response scenarios:

  • Single<T>: Used for responses containing a single View object.

  • Multiple<T>: Used for responses containing a collection of View objects (e.g., list of todos).

4. View Interface (View.java):

The View interface represents a simple data transfer object (DTO) used for transporting data within the Evento Framework. It extends the Payload interface, signifying it carries data. Views are essentially plain data structures that can be serialized for efficient network transmission.

Putting it Together:

By combining these components, you can design clear and well-defined queries along with their expected response structures. Here's an example demonstrating this concept:

Java

In this example, the GetAllTodosQuery expects a response containing a collection of TodoView objects using the Multiple class. This structure clearly communicates the query's purpose and the format of the anticipated response.

By leveraging Evento's Query Payloads and Messages, you can create well-organized and maintainable applications that effectively handle data retrieval across your microservices architecture.

Last updated