Câu hỏi phỏng vấn Clean Architecture
Câu hỏi

What is the difference between Request/Respon...

Câu trả lời

In Clean Architecture, the distinction between Request/Response Models and Entities is crucial for maintaining a clear separation of concerns and ensuring the flexibility and maintainability of the software system.

Entities

Entities are core domain objects that encapsulate business logic and represent the fundamental concepts and rules of the problem domain. They are designed to be independent of any specific use case, framework, or external system. Entities are typically pure data structures that contain both data and behavior relevant to the business logic. They are central to the domain layer and are meant to be stable and reusable across different parts of the application.

For example, an entity might look like this in TypeScript:

typescript Copy
class Product {
  constructor(
    public id: number,
    public name: string,
    public description: string,
    public price: number,
    public available: boolean
  ) {}

  canBePurchased(): boolean {
    return this.available && this.price > 0;
  }
}

Request/Response Models

Request and Response Models, on the other hand, are used to facilitate communication between different layers of the application, particularly between the user interface and the business logic. These models are designed to represent the state of the system at a particular point in time and are often tailored to the specific needs of a use case or an external system. They serve as data transfer objects (DTOs) that carry data between the layers without containing any business logic.

For instance, a request model might be used to capture input data from a user interface, while a response model might be used to format data for presentation to the user. These models are typically defined in the application layer and are mapped to and from entities as needed.

Here is an example of mapping between an entity and a data model in TypeScript:

typescript Copy
// Entity
class User {
  constructor(public id: number, public name: string, public email: string) {}
}

// Model
class UserDataModel {
  constructor(public id: number, public username: string, public emailAddress: string) {}
}

// Mapping function
function mapDataModelToEntity(dataModel: UserDataModel): User {
  return new User(dataModel.id, dataModel.username, dataModel.emailAddress);
}

Key Differences

  1. Purpose:

    • Entities: Encapsulate business logic and represent core domain concepts.
    • Request/Response Models: Facilitate data transfer between layers and external systems.
  2. Location:

    • Entities: Reside in the domain laye...
senior

senior

Gợi ý câu hỏi phỏng vấn

middle

Where should I implement the external API calls logic in Clean Architecture?

junior

How you pass data between modules where they have different models in Clean Architecture?

middle

Explain the control flow of a user interacting with Clean Architecture components?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào