polylith 2024-05-17

Is there some resource out there that explains how to structure components well? I think I messed up a bit with the structure of our polylith monorepo. There is probably too much code in the base and the components are a mishmash of side-effecting code and business logic.

I realized this now as I was looking more into the example repos

In my experience polylith gives you complete freedom in that

But you can draw inspiration from architectures like https://en.wikipedia.org/wiki/Hexagonal_architecture_(software) and/or its derivatives, or https://www.jimmybogard.com/vertical-slice-architecture/ etc.

👍 1
🙏 1

I try to have as much side effect free code as possible, because it's much easier to test and reason about. For example, starting with the i/o operations and then working on that data and then finishing with the i/o (if needed).

do you tend to put the side-effecting code in components and business logic into the base? like hexagonal architecture maybe?

I think the purpose of a base is to expose functionality through some (user) interface, like a cli or a web api

(in hexagonal terms that would fall under the primary adaptor category as far as I remember)

right, makes sense. so you'd put transformational logic into a component?

As I understand the poly recommendation is to put most stuff into components. You can have separate components for your business logic and side effecting code.

👍 1

Create a base as a bridge between your callers and your business logic

But I suppose if there is some business logic that is only ever used in a single base, then there is no harm in keeping it there. It's easy to pull it (or parts of it) out into components later as needed

Our bases are usually less than 50 lines of code in a single namespace, primarily responsible for handing the input from the outer world to an inner component.

✅ 1

Joakim wrote https://medium.com/@joakimtengstrand/understanding-polylith-through-the-lens-of-hexagonal-architecture-8e7c8757dab1 about hexagonal architecture and Polylith. It might be good to check it out if you’ll explore hexagonal architecture

👀 2

I try to put code where you expect to find it. So if you have, let's say, ten functions naturally belong to component x , put them there, regardless of whether two of them have side effects, and eight don't. At least 80% of your functions are side effect free in your component, which is a good thing!

👍 3
👍🏼 1

When we create a new base, it is typically very small and either starts a web server or -main with most of the logic in components. Our only really large bases are where we migrated "legacy" apps and haven't yet refactored them to components. We generally try to keep each component pretty small too and think carefully about their names and make sure they do just one thing. Which is why we have over 150 components 🙂

👍 7
👍🏼 1

Hi guys! I have a component A that use a librairie X. The component A is used in the projects P1 and P2. I want to update the librairie X to the librairie Z but I want only the update on the project P1. For the moment, I created a new component A' (that use the same code of A) but include the new librairie Z and replace the deps in the project P1. Is there an other way to do that?

You could for example create a component X that only contains library x and use that together with A. Then create component Z that only has library z . Then include A and X in P1 + A and Z in P2. Another option is to add x to P1 , and z to p2 and leave it out from A , but then you have to add e.g. x to all other projects.

👍 1