Readit News logoReadit News
erikvdven commented on Keep Pydantic out of your Domain Layer   coderik.nl/posts/keep-pyd... · Posted by u/erikvdven
IshKebab · a month ago
This seems ridiculously over-complicated. This guy would love Java.

He doesn't even say why you should tediously duplicate everything instead of just using the Pydantic objects - just "You know you don’t want that"! No I don't.

The only reason I've heard is performance... but... you're using Python. You don't give a shit about performance.

erikvdven · a month ago
Did I mention anything about performance? If so, my apologies, I'll need to revise the article, because this really has very little to do with performance.

In fact, a reader who emailed me ran into a challenge where, if you have an aggregate with just one entity, for example, Bookcase -> list[Book] , and that list grows significantly, it can lead to performance issues. In such cases, you might even need to consider a solution to improve upon that. But that's a separate topic.

What I was trying to highlight earlier were the whys behind the approach. And based on the feedback over here, it might be a good idea to update the post. I really appreciate all your input.

As for the whys: The less your domain knows about the outside world, the less often you need to change it when the outside world changes. And the easier it becomes for new team members to understand the logic. It also separates your database models from your domain models, which is great IMHO. It makes it easier to change them independent from each other. You could have both, separated domain models and database models or API models and use Pydantic for all these layers, but why would you do that? If you need to make the translation anyways, why not to pure dataclasses?: no extra mental models, no hidden framework magic, just business concepts in plain Python. This does depend on your specific situation however, there are enough reasons to not do this. But if your application grows in size and is not so much a simple CRUD application anymore, I wonder if there are enough reasons to NOT keep Pydantic in the outside layers. So yes, for small simple applications it might be overcomplicated to introduce the overhead when your data stay relatively consistent across layers.

erikvdven commented on Keep Pydantic out of your Domain Layer   coderik.nl/posts/keep-pyd... · Posted by u/erikvdven
nine_k · a month ago
The idea, as far as I was able to understand it, is that you want your core models as dependency-free as possible. If you, for whatever reason, were to drop Pydantic, that would only affect the way you validate inputs from API, and nothing deeper.

This wasn't mentioned, but the constant validation on construction also costs something. Sometimes it's a cost you're willing to pay (again, dealing with external inputs), sometimes it's extraneous because e.g. a typechecker would suffice to catch discrepancies at build time.

erikvdven · a month ago
Exactly. I love the comments by the way! I never expected this would take off like this. The fact that this isn’t clear in the article is excellent feedback, and I'll take it into account when revising it. After a few hours of writing, it's easy to forget to convey the real message clearly.

But you are absolutely right. To add a little: In practice, if a third-party library hardly ever changes and makes life dramatically easier, you can consciously decide to accept the coupling in your domain, but that should be the exception, not the rule.

Pydantic is great at turning large, nested dictionaries into validated objects, yet none of that power solves a domain problem. Inside the domain you only need plain data and behaviour: pure dataclasses already give you that without extra baggage. And that's the main reason to leave it out.

The less your domain knows about the outside world, the less often you have to touch it when the outside world moves. And the easier it becomes for any new team member to adopt that logic: no extra mental model, no hidden framework magic, just the business concepts in plain Python. And exactly what you mentioned: if you ever want to drop Pydantic, you don't need to touch the domain. The less you have to touch, the easier it's to replace.

So the guideline is simple: dependencies point inward. Keep the domain free of third-party imports, and let Pydantic stay where it belongs, in the outside layers.

erikvdven commented on Managing State in Python (a.k.a. Redux?)    · Posted by u/erikvdven
erikvdven · 2 years ago
So I'm not able to delete this message so it seems, but guess I already found a solution to this problem: the observer method, which ironically seems to be also implemented by Redux itself.
erikvdven commented on Front-end is harder than back-end    · Posted by u/erikvdven
devdude1337 · 2 years ago
It’s not harder, it’s more tedious. We’re currently developing a somewhat sophisticated AI/Image Analysis system with OpenCV. While the backend is pretty straightforward it’s still not easy - reading papers about different AI models and implementing them is hard, but the path is clear to see so to say. The client app is using React and already has 3 times the lines of code compared to the backend (which is C++). Frontend has more moving parts that need to be integrated properly. We evaluated other tech for the client, like Qt or even ImGui on OpenGL, but the effort for the client would still be much higher than for the AI services. The only time I experienced a nice, less funky workflow doing frontend, was Delphi 6 and VCL
erikvdven · 2 years ago
Oeh Delphi! That brings back some memories. But I hear what you're saying. I must say front-end has become more mature, but still, it can be quite frustrating so now and then.

More moving part is the right way to say it I guess. And perhaps: you have a user which interacts with it, so there is a lot to keep in mind regarding that as well: show loaders, what happens with y if they hit x and z, etc.

erikvdven commented on Front-end is harder than back-end    · Posted by u/erikvdven
genjii931 · 2 years ago
Isn't this obvious?
erikvdven · 2 years ago
Well, I've seen a lot of discussions which try to prove otherwise. But perhaps it also depends a bit on the level in Software Engineering? If you are new, it might be that back-end feels more complex.

But yeah, front-end even feels harder to debug at some points like the one I mentioned in the OP, at least for me. Not to mention the facts you keep overthinking what might be a good solution in terms of UX (in case that too many DOM elements is the issue of slow performance).

erikvdven commented on Python: Overlooked core functionalities   erikvandeven.medium.com/p... · Posted by u/erikvdven
hwayne · 2 years ago
A couple people already pointed out that you can write `breakpoint()` instead of using `pdb.set_trace()`.

Here's one more trick: you can use `pdb` to run scripts! `python -m pdb foo.py` will run `foo.py` but trigger a breakpoint on the first error.

erikvdven · 2 years ago
That is definitely a neat one! If you are ok with it, I might add that one. I just updated the article already with some of the great comments and tips I recieved over here.
erikvdven commented on Python: Overlooked core functionalities   erikvandeven.medium.com/p... · Posted by u/erikvdven
carabiner · 2 years ago
Would add:

* For dicts, learn .setdefault() vs. .get() vs. defaultdict()

* .sort(key=sortingkey)

* itertools groupby, chain

* map, filter, reduce

erikvdven · 2 years ago
Perhaps for another article? :) But thanks, definitely a nice list! My purpose for now was to keep the article 'digestible' as well.

u/erikvdven

KarmaCake day243July 2, 2014View Original