Beyond Fat Models: An Introduction to DCI Architecture
Why object-oriented code gets tangled over time, and how Data, Context, and Interaction (DCI) separates what a system is from what it does.
If you have ever worked on a mature web application or enterprise codebase, you have probably seen the Fat Model problem firsthand.
It starts innocently enough. You create an Account or User class to represent your data. Six months later, that single file is 2,000 lines long – bloated with methods for money transfers, password resets, PDF exports, tax calculations, and admin audits. Everything that touches a User ends up living inside User.
This happens because traditional Object-Oriented Programming (OOP) merges two fundamentally different things into a single construct (the class):
- What the system is: The stable domain state and data structures.
- What the system does: The dynamic workflows and use cases that change every sprint.
In 2009, Trygve Reenskaug (the inventor of MVC) and James Coplien introduced Data, Context, and Interaction (DCI) to solve this exact problem.
The Core Idea Behind DCI
DCI splits your architecture along mental lines that match how both users and developers naturally think about software:
- Data (What the system is): Dumb domain objects. They hold state and enforce basic invariants (like ensuring a balance never drops below zero), but they know nothing about business workflows or other domain objects.
- Roles (How objects collaborate): Stateless behaviors that an object temporarily adopts during a specific use case. In a money transfer, one
Accountplays the role of Source Account, while another plays the role of Destination Account. - Context (When and where it happens): An orchestrator for a single use case. The Context finds the right Data objects, binds them to their temporary Roles, and kicks off the interaction.
- Interaction (What the system does): The readable, step-by-step algorithm written in terms of Roles talking to each other (
source.transferTo(destination, amount)).
Why DCI Struggled in Traditional OOP – and Shines in Nim
In languages like Ruby, C#, or Java, implementing DCI requires fighting the language’s class system. You have to dynamically inject methods into object instances at runtime (risking subtle bugs and performance hits) or wrap objects in heavy adapter classes.
Nim sidesteps this entire struggle by design.
Because Nim separates object data definitions from proc behaviors out of the box – and connects them with Uniform Function Call Syntax (UFCS) – you do not need runtime metaprogramming or heavy frameworks to do DCI. Instead, Nim’s distinct types let you bind Data to Roles at compile time with zero runtime overhead.
What We Will Build in This Series
Over the next two parts, we will build a working DCI architecture in clean, idiomatic Nim without a single external dependency:
- Part 3: Implementing DCI in Nim (The Core Pattern): We will build the classic DCI Money Transfer use case using
distincttypes, UFCS, and module-level lexical scoping. - Part 4: Polymorphic Roles & Production Patterns: We will scale the pattern using Nim
concepts so different data types (like checking accounts and credit cards) can play the same Role, and compare Nim’s compile-time approach with dynamic languages like Perl.
