Core Code vs Infrastructure Code: A Comprehensive Overview

Amine
3 min readMar 11, 2023

--

If your application is supposed to live longer than, say, two years, then decoupling from infrastructure is a safe bet. Surrounding infrastructure like frameworks, remote web services, storage systems, etc. are likely to change at a different rate than your domain model and use cases. Whatever happens in the world of the technology surrounding your application, your precious core code won’t be disturbed by it. Both can evolve at their own speed. Upgrading to the next version of your framework, migrating to a different storage backend, or switching to a different payment provider won’t cost as much as it would if core and infrastructure code were still mixed together. Dependencies on external code or systems will always be isolated and if a change has to be made, you’ll know immediately where to make it.

Core code:

Core code is the heart of an application. It’s the code responsible for the main functionality, and it doesn’t depend on any external systems or specific environments to run. This means that the core code can be executed on any platform, and it doesn’t require any dependencies that are designed to run in a specific context only. In other words, the core code is independent and doesn’t rely on anything outside of itself.

Examples of code that needs external dependencies to run.

public function callYouCanApi(): void
{
/*
∗ To run this code, we need an internet connection,
∗ and the API of api.youcan.shop should be responsive .
*/

$ch = curl_init("https://api.youcan.shop");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
}
public function usesGlobalState(): void
{
/*
* Here we rely on global state, and we assume this
* method gets executed as part of an HTTP request.
*/
$host = $_SERVER[’HTTP_HOST’];
$this->doSomethingWith($host);
}

Example of a core code.

class UserService
{
private UserRepository $userRepository;
public function __construct(UserRepository $userRepository)
{
$this−>userRepository = $userRepository;
}

public function create(
string $email,
string $password
): void {
$request = User::requestAccess(
EmailAddress::fromString($email),
Password::fromString($password)
);

$this->userRepository−>save($request);
}
}

Infrastructure code:

Infrastructure code, on the other hand, is responsible for supporting the operation of the application. It includes code that interacts with external systems such as databases, web servers, and other applications. Infrastructure code is designed to run in a specific context, and it requires external systems to be available to function correctly.

Example of Infrastructure code.

interface Connection
{
public function create(string $table, array $data): void;
}

class UserRepository
{
private Connection $connection;
public function __construct(Connection $connection)
{
$this−>connection = $connection;
}
public function create(
string $username,
string $password
): void {
$this−>connection−>create(
’users’,
[
’username’ => $username,
’password’ => $password
]
); }
}

Relationship between Core Code and Infrastructure Code:

The result should be that none of the core code depends on infrastructure code on either side. At the same time, any infrastructure code would be able to call core code. There are no special requirements for doing so. Both ingredients combined result in code that is completely portable. It provides a clear view on the application’s use cases, without any distortions caused by infrastructural concerns. It can be easily tested, in complete isolation, without any special setup.

References:

Advanced Web Application Architecture

https://www.pewresearch.org/internet/fact-sheet/internet-broadband/

--

--