Ports & Adapters – Software Architecture

Alistair Cockburn’s description of a software architecture that divides the “inside” and “outside” of an application was something I first ran into quite a while ago, and at first I didn’t quite grok why it was better than my current experiences with N-Tier.

However, with time, experience and quite often pain, perspectives can shift, and over the last couple of years things fell into place. It has been almost exclusively a weapon of choice I use when building a new application.

As with anything that is different from the masses of opinion, it can be quite difficult to explain to fellow developers and teams how to implement the pattern. This can be due to any number of problems, for example sheer wrong headedness, but most often it is just due to the fact that to see the benefits of an architectural style such as ports and adapters, you really need to have your A game already in terms of understanding the following

  • System/Application/Domain (Business Logic) Boundaries
  • Dependency Inversion (by this I mean understanding what is meant by high level and low level, not Dependency Injection, see Fowler)
  • Abstractions (this is not adding an interface to every class and having the interface live with the implementation !)
  • Coupling (be comfortable with statics and concrete classes where appropriate, not everything needs to be an instance and, see above, not everything needs to be an abstraction
  • Unit testing (again, this is not having a matching test fixture for every class, if you’re a TDD practitioner, most of the classes that exist when you’re done are refactorings and implementation details of your original starting point. This is especially true if you use outside in development appropriately, see Seemann . For what it’s worth, outside in development is what I agree that Kent Beck generally meant when he bought forth the TDD concept)

But I’m off on a tangent (as usual).

I have often been asked to provide an easy .NET example for people to use to familiarize themselves with Ports and Adapters, and it has been on my list of things to get do… The only problem with my list of things “to get to” is it’s quite long, and my sons are pretty good at nerd sniping me with things like “lets write a game in Scratch” or, more lately, lets write a minecraft mod..

Now that I’ve sat down and reflected over the last few intense months of starting a bunch of projects at Coolblue for an integration project that are all using Ports and Adapter style architecture, I thought it might be time to “take the show on the road” so to speak.

So I had started putting a presentation together and then figured I might as well find a forum other than my work colleagues, after all there is only so much they can take of me muttering on about Mikado methods, boundaries, idempotency, concurrency and the other things that get in my bonnet.

I’ll be talking at the XP Days event in the Ukraine, and this gave me the motivation needed to start creating a git repo with a small sample .NET application demonstrating a simple Book Ordering domain, and various examples of ports and adapters being wired up.

There are examples of adapters that use an IoC container internally, which is nice if you have some complicated composition going on that you don’t want to do by hand, and there are examples of upstream ports that invoke various use cases from the domain, such as allowing a message on a RabbitMq queue to invoke the logic for placing a book order request.

The example also focuses on the situation where you might have common business logic but have to support multiple different hardware configurations for clients, not something we really do at Coolblue since we are an internal software company, but the ability to write a second adapter to migrate from say, a document database to a relational database, or from direct calls to a database to REST calls via a more suitable service.

The code is available here and is by no means finished as an example yet, there is also an extensive disclaimer about UNDERSTANDING the example and not copy/pasting things out of it into production code. The example focuses directly on ports and adapters and ignores millions of other things that are critical to any MVP going into battle.

https://github.com/nrjohnstone/ports-adapters-examples

 

BookOrdersPortsAdaptersOverview

 

Also, if anyone wants me to come and run the presentation through at a user group I’d be only too happy.

 

Tagged with:
Posted in Article
One comment on “Ports & Adapters – Software Architecture
  1. Johannes says:

    Ace dude, dont have a MS VM ATM so will try n clone ya repo and make it work on MAC 🙂 Great read!

    Like

Leave a comment