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

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

Câu trả lời

In Clean Architecture, passing data between modules that have different models involves using Data Transfer Objects (DTOs) and mappers to ensure that each layer remains decoupled and adheres to the Dependency Rule. Here’s a detailed explanation of how this can be achieved:

1. Understanding the Layers

Clean Architecture is structured into several layers, each with its own responsibilities:

  • Entities Layer: Contains the core business logic and domain entities.
  • Use Cases Layer: Implements the business rules and application logic.
  • Interface Adapters Layer: Converts data from the use cases to a format suitable for the outer layers (e.g., UI or database).
  • Frameworks and Drivers Layer: Contains the external agents like UI, database, and web frameworks.

2. Using Data Transfer Objects (DTOs)

DTOs are simple objects used to transfer data between layers without exposing the internal details of the domain entities. They help in maintaining the separation of concerns and ensure that changes in one layer do not affect others.

3. Implementing Mappers

Mappers are used to convert data from one model to another. Each layer can have its own model, and mappers handle the conversion between these models.

Example Workflow

Here’s a step-by-step example of how data can be passed between modules with different models:

Step 1: Define Models in Each Layer

  • Data Layer Model: Represents the data as it is stored in the database.
    data class UserEntity(
        val id: String,
        val name: String,
        val email: String
    )
  • Domain Layer Model: Represents the core business logic.
    data class User(
        val id: String,
        val fullName: String,
        val emailAddress: String
    )
  • Presentation Layer Model: Represents the data as it is displayed in the UI.
    data class UserViewModel(
        val id: String,
        val displayName: String,
        val contactEmail: String
    )

Step 2: Create Mappers

  • Data to Domain Mapper:
    class UserEntityToDomainMapper : Mapper<UserEntity, User> {
        override fun map(input: UserEntity): User {
            return User(
                id = input.id,
                fullName = input.name,
                emailAddress = input.email
            )
        }
    }
  • Domain to Presentation Mapper:
    class UserDomainToViewModelMapper : Mapper<User, UserViewModel> {
        override fun map(input: User): UserViewModel {
            return UserViewModel(
                id = input.id,
                displayName = input.fullName,
                contactEmail = input.emailAddress
            )
        }
    }

Step 3: Use DTOs for Data Transfer

When data needs to be passed from one layer to another, it is first converted to a DTO, transferred, and then converted back to the appropriate model in the receiving layer.

  • Use Case Layer:
    class GetUserUseCase(
        private val userRepository: UserRepository,
        private ...
junior

junior

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

middle

What is the role of the Controller in Clean Architecture?

middle

What are Use Cases in Clean Architecture?

junior

What is an Entity in Clean Architecture?

Bình luận

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

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