Monday, February 11, 2019

Obey the Testing Goat: Python Architecture Stuff: do we need more?

I've been learning lots of new stuff about application architecture recently, and I want to get a sense from you, dear reader, of whether these ideas are of interest, and whether we should try and build some more resources (blogs, talks, etc) around it.

How should we structure an application to get the most out of our tests?

For me it all started with this question. At the end of my book I concluded on a chapter discussing how to get the most out of your tests, on the tradeoffs between unit, integration and end-to-end tests, and made some vague, flailing gestures towards topics I didn't really understand: ports and adapters, hexagonal architecture, functional core imperative shell, the clean architecture, and so on.

Since then I've managed to fall in with a tech team that are actively implementing these sorts of patterns, in Python. And the thing is, these architectural patterns are nothing new, people have been exploring them for years in the world of Java and C#. They were just new to me... and I may be over-reaching from my own experience here (I'm interested in your reaction), but they are perhaps new to the Python community in general?

It does feel like, as we mature, more and more of what were once small projects and plucky startups turn into complex business and (whisper it) enterprise software, so this stuff is likely to get more and more salient.

I came to it initially from the angle of testing, and the right kind of architecture really can help you to get the most out of your tests, by separating out a core of business logic (the "domain model") and freeing it from all infrastructure dependencies, allowing it to be tested entirely through fast, flexible unit tests. At made it finally felt like the test pyramid was an achievable goal rather than an impossible aspiration.

Classic books on the topic (All Java.)

The classic books (Evans on DDD and Fowler on Architecture Patterns, are classic, and anyone with an interest in this stuff should read them, but if you're anything like me, then wading through all that public static void main AbstractFactoryManager gubbins is a bit wearing. Maybe some more lightweight, Pythonic intros would make it all feel a bit more plausible, a bit less enterprise-architecture-astronaut-ey?

Some existing resources in the Python world:

Made's chief architect, the Venerable Bob, has written a 4-part blog series on the way we do things here, which I really enjoyed when I first started. They're a quick, practical intro to the basic concepts of DDD, ports and adapters / dependency inversion, and to some extent, event-driven architecture. And all in Python. (trigger warning: type hints).

  1. Ports and Adapters with Command Handler pattern in Python
  2. Repository and Unit of Work Pattern in Python
  3. Commands and Queries, Handlers and Views
  4. Why use Domain Events?

There's more on io.made.com but those are the main four. We'd love to get some feedback on them, what's covered well, what could do with more explanation, and so on...

And: a very timely release from last Christmas, check out Clean Architectures in Python by Leonardo Giordani. It's really two books in one, part one being an intro to TDD, but part 2 has four chapters introducing very similar patterns to the ones I'm talking about here.

I also enjoyed a talk from about a year ago by David Seddon The Rocky River, how to architect your Django monolith, showing someone else starting to think about how we go beyond the basic Django models/views/templates architecture.

There's lots more at this listing of DDD resources by Valentin Ignatev, which I came across recently on twitter. It feels like something is in the air.

Call to action: is this stuff interesting?

Bob's already had some good feedback to his blog posts, and Leonardo has had some good initial sales, so I'm sensing some interest from the Python community out there, but I'd like to sanity-check it.

  • Does this stuff sound interesting or relevant? Do you want to hear more?
  • Are you doing Python stuff that's getting beyond the bounds of "basic webapp development" or "data pipeline"? Are you finding it difficult to write fast unit tests? Are you starting to want to disentangle your business logic from whichever framework you use?
  • Are you doing DDD or using any of these classic patterns with Python already? Do you maybe have all the answers and want to tell me about it? Or maybe just some answers and things that have worked well for you?
  • Do you think this stuff all sounds hella abstract and pointless? Maybe Made.com is a bit of an outliner in the Python world, in that we're writing logistics/ERP/enterprisey software in Python, and it all feels very different from what you do day-to-day?
  • What do you think the Python / dynamic languages community would most benefit from in terms of new guides to these topics?

I'd love to hear from you. Comments are open, or hmu on twitter, @hjwp

Did I say read the classics? Read the classics.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...