Sobes.tech
Senior

What is DCI?

sobes.tech AI

Answer from AI

DCI (Data-Context-Interaction) is a design pattern that proposes to divide application logic into three interacting parts:

  • Data: Objects representing the subject area (e.g., Account, User). They do not contain business logic, only data and basic access operations.
  • Context: An object describing a specific use case or interaction between objects. It links data with roles and coordinates the execution of the interaction.
  • Interactions / Roles: Behavior that objects (Data) acquire within a specific context. Roles define how objects interact with each other to accomplish a task.

The main idea of DCI is to separate operational logic (business processes) from data objects, making them cleaner and more reusable. Logic is concentrated in contexts and roles, which facilitates understanding and modifying specific use cases.

Advantages of DCI:

  • Improved readability: Code related to a specific scenario is located in one place (the context).
  • Reduced complexity of data objects: Data objects remain "lean," containing only state.
  • Reusability of data: Data objects can be used in various contexts with different roles.
  • Ease of testing: Scenarios (contexts) can be tested in isolation.

Example (conceptual): Transferring money between accounts.

  • Data: Account objects (contain balance, deposit, withdraw methods).
  • Context: TransferMoneyContext. It links two Account objects (source and destination) and defines roles.
  • Roles:
    • SourceAccount (Role for the source account, can withdraw).
    • DestinationAccount (Role for the recipient account, can deposit).

In TransferMoneyContext, the logic will be described: take money from SourceAccount, if possible, and deposit it into DestinationAccount.

DCI is not tied to a specific language, but its implementation can vary. In object-oriented languages, roles are often implemented through mixins, traits, or interfaces/classes that data objects "accept" in the context.