In my opinion Repository is an anti-pattern while developing in Python, it's great in other languages but it's not so good in python. The sole reason for it is asyncio module. If you're planning to use repositories and allow usage of Async APIs, your domain logic must be async to (at least functions which are meant to access repositories) My solution was to never use repositories inside domain layer.
That's not correct, for example: async def my_service_layer_func(request): my_domain_object = await some_repo(request.resource_id) my_domain_object.some_completely_synchromous_domain_method() await some_repo.save(my_domain_object) I've been using the repo pattern for abt 2.5 years in prod, in an async codebase, and my domain layer is completely synchronous. True, if you want to inject repos into your domain layer your domain will have to be async, but that's an antipattern anyway. So that's essentially your code telling you you're trying to implement a bad idea.
@@wesselbindt If you read the book mentioned in this Talk (which is "Cosmic python") you'll see that authors suggest to use repositories in domain service layer. Various articles on DDD in python suggest the same, so it's pretty much considered a "pattern", not an "anti-pattern". It is positioned as a way do decouple IO from our domain layer, so services in domain-layer are not bothered by the concrete implementations of storages, and whole point of repos is to inject them into domain layer. Which couples our domain layer on concrete IO implementation, which is bad. Personally I use repositories as a way to construct business objects in my application layer and pass them to my domain layer, which decouples domain layer from the chosen IO model.
Excellent talk! I will certainly rewatch this, and I will recommend it to any Python developer I talk to.
For me, this has been one of the best talks of pycon 2023 so far. I’m wondering where can I get the slides.
The sound level is way too low. Otherwise it’s an excellent talk.
In my opinion Repository is an anti-pattern while developing in Python, it's great in other languages but it's not so good in python. The sole reason for it is asyncio module.
If you're planning to use repositories and allow usage of Async APIs, your domain logic must be async to (at least functions which are meant to access repositories)
My solution was to never use repositories inside domain layer.
That's not correct, for example:
async def my_service_layer_func(request):
my_domain_object = await some_repo(request.resource_id)
my_domain_object.some_completely_synchromous_domain_method()
await some_repo.save(my_domain_object)
I've been using the repo pattern for abt 2.5 years in prod, in an async codebase, and my domain layer is completely synchronous. True, if you want to inject repos into your domain layer your domain will have to be async, but that's an antipattern anyway. So that's essentially your code telling you you're trying to implement a bad idea.
@@wesselbindt If you read the book mentioned in this Talk (which is "Cosmic python") you'll see that authors suggest to use repositories in domain service layer. Various articles on DDD in python suggest the same, so it's pretty much considered a "pattern", not an "anti-pattern".
It is positioned as a way do decouple IO from our domain layer, so services in domain-layer are not bothered by the concrete implementations of storages, and whole point of repos is to inject them into domain layer. Which couples our domain layer on concrete IO implementation, which is bad.
Personally I use repositories as a way to construct business objects in my application layer and pass them to my domain layer, which decouples domain layer from the chosen IO model.