Skip to main content

Humble Objects at the Process Boundary

CHACK diagram. Inside a process boundary sit the Core with its I/O abstractions, the Humble Objects at the boundary, the Composition Root, and the unit tests with their test doubles. Integration tests straddle the process boundary, because they may exercise the system under test inside or outside the test runner process. End-to-end tests sit to the right, outside the boundary, because they usually start the system under test in another process, for example with Docker Compose or Aspire. The Humble Objects exchange data at runtime with third-party systems such as databases, web services, and message brokers. Two kinds of compile-time arrows are used: an open arrowhead means one category references another, and a dashed arrow with a hollow triangle means one type implements or derives from another. The Humble Objects and the test doubles both implement the I/O abstractions. The Core references the I/O abstractions. Unit tests reference the Core and their test doubles. Integration and end-to-end tests reference the Humble Objects, the Composition Root, and the third-party systems. The Composition Root references the Core and the Humble Objects. Every category may depend on the frameworks and libraries they reference, so those arrows are left out. The Humble Objects are highlighted.Process boundaryFrameworks & LibrariesBase Class Library, HTTP Server Frameworks, Data Access,Validation Frameworks, Test Frameworks, …Third-PartySystemsDatabasesWebServicesMessageBrokers…HumbleObjectsI/OAbstractionsCoreAutomatedTestsUnitTestsTestDoublesIntegration TestsE2E TestsComposition Rootreferencesimplements /derives fromI/O call at runtime

All four categories may depend on the frameworks and libraries. Those arrows are left out to keep the number of arrows manageable.

Why the boundary needs its own category​

No application does anything useful without I/O. Databases, the file system, web services, message brokers, printers, CRMs, ERPs, SAP, you name it. Every one of them lives outside your process, and reaching them costs usually a billion times more time than an in-memory call. The Automated Tests page has the numbers.

Cost is only half of it. I/O is also the main part of your code that causes exceptions and that is the hardest to exercise in Automated Tests: a third-party system must be running, populated with the right state before the test and reset afterwards. Provoking failures on demand can be hard or outright impossible. Code that mixes business rules with I/O inherits all of that difficulty, entangling both is the main reason for "Spaghetti Code" in my opinion.

So CHACK isolates I/O from the Core. At the process boundary sit Humble Objects: for each use case, they represent the glue between your Data Access technology and the Core. Each Humble Object should talk to one third-party system. They implement an I/O abstraction owned by the Core, which means you can introduce a Test Double for it in Unit Tests and stay in-memory.

The responsibilities of a Humble Object​

  1. It performs the I/O calls. You usually use a data access technology like an O/R mapper, an HTTP client, a broker client, invoking a CLI process, or proprietary SDKs/drivers to access the third-party system. Aim for one I/O call per method on the Humble Object, though in certain use cases, more than one call can occur.
  2. If your data access technology supports Change Tracking, some methods on the Humble Object may be required to add or remove entities from the O/R mapper's Unit of Work. The capabilities of your Data Access technology shapes the API for the Humble Object.
  3. It maps the data. Outgoing parameters get translated into whatever the third-party system expects, and incoming data gets translated into the types the Core wants back. This can involve catching exceptions which then can be mapped to, e.g., the Result Pattern or wrapped in another exception the Core understands.
Light.PortableResults

I have implemented my own Result Pattern library called Light.PortableResults. Check it out on GitHub!

That is the whole list. Everything else like decisions, validation, calculation, orchestration, or logging belongs to the Core.

Two notes on mapping the data:

  • The framework does it for you. An O/R mapper materializing entities from a query, or a serializer turning a response body into a DTO, is mapping you did not write. Cover it in Integration and End-to-End Tests.
  • You write it yourself. You might simply leave it in the Humble Object as a private method. If it is complex enough to deserve its own tests and you are able to unit-test it easily, move it into a static method on a different type, belonging to the Core. What stays in the Humble Object is the call plus a delegation to the mapper.

The rule of thumb: make the Core as large as possible and the Humble Object as small as possible. If you find yourself wanting a test for something inside a Humble Object, that something likely belongs in the Core.

Guidelines​

  • Keep them boring. Avoid branches and loops. A Humble Object that needs an if for anything other than a precondition check is usually hiding a decision that belongs in the Core.
  • One Humble Object per third-party system, per vertical slice. Where the target system supports transactions, a Humble Object typically spans exactly one of them (representing a Unit of Work or one connection to the third-party system). Do not access two different systems within one Humble Object.
  • One method per interaction. Target one I/O call per method. On rare occasions, multiple I/O calls per Humble Object method can make sense. If the technology supports asynchronous calls, the method is asynchronous.
  • No validation. A Humble Object returns DTOs or domain objects; the Core validates them if validation is required. Data coming back from a third-party system is input like any other.
  • Unify at the boundary, if you must. If the same data lives in two systems with slightly different shapes - for example, two DBMS with different table layouts - the Humble Objects may normalize both into the same Core type. That is mapping, and it is allowed.
  • Release resources deterministically. Humble Objects usually own something that has to be released: a connection, a transaction, a stream, a file handle. Then the Humble Object can be released, too, and its I/O abstraction includes that operation, so that a caller can release it without knowing the implementation. In most applications, the Composition Root takes care of this at the end of a request or message; without a DI container, the caller does it. Clients and Sessions shows both.
  • The same rules apply to UI code. A view is a Humble Object: it renders and it forwards user interactions. Keep bindings simple, avoid multi-bindings and view-specific converters that carry logic, keep animation triggers dumb, and put no logic in printed reports. Anything you move out of the view is something you can unit-test.
  • Return results, not deferred queries. A query object that the caller can still extend and execute later moves the decision about what is queried into the Core. When the Humble Object materializes the results itself, the I/O stays in one place.

Clients and Sessions​

A Humble Object that the Core uses comes in one of two flavors, depending on who controls the transaction:

  • A Client is used when the Core does not control transactions. A Client may still use a transaction internally - for a single query, or for its whole lifetime - but it never hands it to the Core.
  • A Session is used when the Core controls a transaction. It offers a method to commit, and releasing it without committing rolls back all changes.

So a Humble Object that only reads data and offers no way to commit is a Client, not a Session. Clients and Sessions shows both in detail.

Scope and sharing​

Each vertical slice gets its own Humble Object per third-party system. This matters most when transactions are involved, because a Session then marks the transaction boundary of exactly one feature.

You may know a different approach from the Repository pattern, where a single abstraction collects all operations that belong to an entity across different features. Such an abstraction is shaped by the data instead of by its consumer, which works against the Dependency Inversion and Interface Segregation Principles: every consumer depends on members it doesn't need, and every Test Double has to deal with them. Clients and Sessions shows how the two approaches compare.

Two or more slices can share the same Humble Object when they need exactly the same abstraction. If their needs drift apart later, you split it again.

Separate Humble Objects don't have to mean duplicated code. Common implementation code - queries, filters, projections, mapping - can move into static methods that each Humble Object calls. Hierarchy and sharing helps you find the right folder for them. Static methods that perform I/O belong to the Humble Object category; static methods that run in memory may belong to the Core.

The local file system is I/O, too​

Reading and writing files is I/O, even though the file system is always there. The same goes for libraries that work on files on behalf of the Core, like document converters or image processing libraries. It's worth putting such access behind an I/O abstraction, too. Your Unit Tests may still touch the local file system (see Automated Tests), but a Test Double lets you simulate the errors that are hard to provoke on a real disk - a locked file, a full drive, a corrupt document.

Supporting types​

Not every type in this category is called by the Core. A database context subclass, a base class for Humble Objects, a static helper method that performs I/O, or a startup task that the Composition Root runs to create database indexes - all of them perform I/O, so they are Humble Objects, too. They don't need to implement an I/O abstraction of the Core, and the guidelines above help them stay just as simple.

Where the pattern comes from​

Michael Feathers published The Humble Dialog Box in 2000. He had built a modal dialog with the logic baked into the window, then found he could not test it without spinning up a UI thread and initializing the whole UI framework first. His fix was to move the logic into a separate class and leave the dialog with nothing but its interactions with the framework - the dialog became humble.

xUnit Test Patterns generalized this into the Humble Object pattern: the Humble Object holds the logic that is hard to unit-test and delegates everything else to a testable component.

Each time the Humble Object is called by the framework, it delegates to the testable component. If the testable component needs any information from the context, the Humble Object is responsible for retrieving it and passing it to the testable component. The Humble Object code is typically so simple that we often don't bother writing tests for it because it can be quite difficult to set up the environment need to run it.

Robert C. Martin arrives at the same construction in chapter 23 of Clean Architecture, where Humble Objects at the process boundary are what keeps business logic free of I/O.

In .NET Practice

Clients and Sessions in .NET shows Humble Objects with Entity Framework Core and a typed HTTP client, and base classes that keep them short.