Slide 4
Slide 4 text
Python
でDDD
をやってみた感想 4
entities = self._repo.get_all(self, post_id)
return entities
DomainService
(複雑な場合に使⽤。現状は使⽤していない)
ドメインロジックの組み⽴てを⾏う。
ApplicationService
やDomainModel
でも実装が難しい複雑なケースで使⽤する
Infrastructure
DB
や外部サービス(File Access, Access, ORM, etc...)
にアクセスして永続化を
担当する層。
repositories.py
インフラ層
ドメインオブジェクトの永続化層の実装を伴うクラス
Entity
クラスを返す
class PostRepository:
@classmethod
def save(cls, post: PostEntity) -> Optional[Post]:
post_model = Post.objects.update_or_create(
id=post.id,
defalults={
"title": post.title,
"content": post.content
}
)
return PostEntity.from_model(post_model)
@classmethod
def find_by_id(cls, post_id: int) -> Optional[PostDto]:
post_model = Post.objects.get(id=post_id)
return PostEntity.from_model(post_model)
@classmethod
def get_all(cls, post_id: int) -> Optional[PostDto]:
post_entities = []
for post_model in Post.objects.all:
entity = PostEntity.from_model(post_model)