-1

I am concerned about overengineering my code when thinking about Ports and Adapters architecture.

Two very common examples of the system contacting with the outside world are my HTTP stack and local filesystem. These are clear situations when the app is reaching out to the outside world, and testing will require certain amount of control and/or mocking.

My first though was: "Maybe I should have a central service or singleton and make sure any HTTP call goes through it"

For testing it would be amazing because I could force any test to crash if an unexpected call to the outside world is done without a mock or at least explicit concert by the developer. This also standardizes a single way to mock HTTP calls.

On the other side it feels like I am reinventing the wheel. I am a Javascript developer and the native library is not so good. Most people use a third party library which will spread as a dependency throughout the project.

My current approach is kind of re-inventing the wheel. I do create the abstraction layer (a service singleton) but keep it light using a third party library. The goal is trying to "contain" the spread of 3rd party library code over the project using a thin self made facade. Obviously things with a larger scope are an exception. For example abstracting away database access tends to cause more pain than glory.

Are self made abstractions for HTTP and local file system access a clear example of Ports/Adapters or just overengineering?

SystematicFrank
  • 14,622
  • 5
  • 51
  • 95
  • 1
    There is no "red line" WRT overengineering. Over a big, fuzzy, grey line, and lots of opinions. – Flimzy Feb 22 '21 at 09:39
  • I was counting that asking for the specific case of "http calls" and "local filesystem" would be enough to identify an anti pattern or assert "those two are clear cases of hexagonal architecture in action" – SystematicFrank Feb 22 '21 at 09:49
  • The direct answer to your question: "Are self made abstractions for HTTP and local file system access a clear example of Ports/Adapters or just overengineering?", is "No. It's not a clear example of ports/adaptors, and it's not a clear example of overengineering. It's an example of one possible approach to a broad set of problems." – Flimzy Feb 22 '21 at 10:26
  • Business logic should not be coupled to the HTTP protocol or the file system. I'm not sure whether that answers your question; but it's an easy statement to make based on hexagonal architecture. – jaco0646 Feb 22 '21 at 16:07

1 Answers1

2

That dilemma (creating your own "gateway" for http access) has to do more with reusability and DRY than with hexagonal architecture.

Hexagonal architecture says "put a technology agnostic port in your app, for talking to the outside world item, and then put an http adapter outside your app, for translating".

The point is... do you have many apps with an http adapter? In this case maybe it's worth it to abstract away the http adapter as kindof a custom gateway.

choquero70
  • 3,332
  • 2
  • 24
  • 40