liveBook · Manning

Port

As explained in gRPC Microservices in Go

A network port is a numbered communication endpoint that identifies a service or process on a networked host.

A port is an interface that defines how an application’s business logic interacts with something outside it. In hexagonal architecture, ports are contracts; adapters provide concrete implementations. This separates the application core from transport, database, and other service details. section 4.1.3 section 4.1

Direction matters. A driving port exposes actions that an external actor can request. A driven port defines a capability the application needs, such as saving an order or charging a customer. An adapter translates between its port and a specific technology. section 4.1.1 section 4.1.4

For an Order service, keep each contract focused on its role:

  • APIPort exposes PlaceOrder to an incoming adapter, such as gRPC. section 4.2.4 section 4.2.1
  • DBPort supplies Get and Save operations without making application logic depend on a particular database adapter. section 4.2.4
  • PaymentPort supplies Charge(*domain.Order) error so placing an order can include a payment step without depending on a specific payment implementation. section 5.2.1 section 5.2.3
package ports
 
import "github.com/huseyinbabal/microservices/order/internal/application/core/domain"
 
type APIPort interface {
    PlaceOrder(order domain.Order) (domain.Order, error)
}

The Go APIPort listing shows a contract expressed as one business operation, PlaceOrder, with a domain object as its argument and result. section 4.2.4

Define the interfaces in internal/ports, implement the business logic in the application, and place concrete integrations in adapters. At startup, construct the database adapter, pass it to the application, then pass the application to the gRPC adapter. An adapter can be replaced when it satisfies the same interface; tests can likewise substitute mocks for DBPort and PaymentPort to control dependent-system behavior. section 4.2.1 section 4.2.7 section 7.2.3

Do not confuse an architectural port with a network port. APPLICATION_PORT is the numbered endpoint on which the Order service runs; it configures the gRPC adapter, rather than defining an application contract. section 4.2.7

How gRPC Microservices in Go covers it

How deep it goes
Works through Go interface signatures, adapter placement, and startup wiring; does not examine network-port protocol internals.
Examples
Uses PlaceOrder, database Get and Save contracts, and a payment Charge contract in an Order service.
What it assumes
Assumes familiarity with Go interfaces and service dependencies while defining the architectural role of ports.
What you can do after
Build it from scratch as Go interfaces, inject matching adapters, and substitute mocks when testing dependent operations.
How it connects
Connects hexagonal architecture, adapters, dependency injection, domain models, gRPC, databases, payment services, and mocks.

Also explained in

Compare how 3 books explain port →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of gRPC Microservices in Go. The section links point to the parts of the book it draws on.