Course contentsShow
← Software Engineering
Lesson 1267 of 1,93338. Layered and Modular ArchitectureFree lesson

Implementing Adapters

Learn to create adapters that translate between external systems and your application's domain model.

Implementing Adapters

You'll learn how to build adapters that connect external systems to your application while keeping your core logic independent.

What Is an Adapter?

An adapter is a piece of code that translates between the language your application's domain model speaks and the language an external system uses. Think of it like a power adapter when traveling abroad—your laptop doesn't change, but the adapter lets it work with different electrical outlets.

The Core Concept

Your application defines ports (interfaces) that describe what it needs—like "I need to save users" or "I need to send notifications." Adapters implement these ports using specific technologies (a database, an email service, a REST API).

The key rule: your domain logic depends only on the port interface, never on the adapter implementation. This keeps your core business rules clean and technology-agnostic.

Structure of an Adapter

An adapter typically:

  1. Implements a port interface defined by your application
  2. Wraps an external system (database driver, HTTP client, message queue)
  3. Translates data between your domain model and the external format
  4. Handles technology-specific concerns (connection management, error handling, serialization)

For example, if your application defines a UserRepository port, you might create a PostgresUserAdapter that implements this interface, translates domain User objects into SQL statements, handles database connections, and converts query results back into domain objects.

Why This Matters

Adapters isolate change. When you swap databases or integrate with a new payment provider, you write a new adapter—your core business logic remains untouched. This makes your system testable (use mock adapters) and flexible.

Key Takeaway: Adapters are translation layers that implement your application's port interfaces, converting between your domain model and external system formats while keeping your core logic independent of infrastructure details.