I'm studying Clean Architecture with python and got into this "problem" of choosing where to implement a rule (that I considered a Business Rule, maybe I'm wrong)
I have 2 Entities
@dataclass
class A:
bs: List[B]
id: Optional[str]
# ...
def add_b(self, b):
exists = next((x for x in self.bs if x.utc == b.utc), False)
if exists:
# treat
else:
self.bs.append(b)
@dataclass
class B:
utc: Datetime # unique in the same A
id: Optional[str]
# ...
I decided to consider A
an Aggregate since it would always work with a list of bs
(at least that seemed the "right" thing to do after some reading about Aggregates), what would make me have only 1 repository: ARepository
and I have this Usecase:
class AddBtoA:
def __init__(self, a_repo: ARepository):
self.a_repo = a_repo
def execute(self, a_id, b_data):
a = self.a_repo.get(a_id)
b = B(**b_data)
a.add_b(b)
self.a_repo.save(a)
Problem
For each A
I'll be adding a new B
at a rate of ~5 minutes, so the query will grow big fast. It looks like checking if a B
with the same utc
already exists would better be done at SQL/Repository level with a SELECT
for technical reasons and simplify the A.add_b
method to directly append the list, but wouldn't this transfer Business Logic to an outer layer?
Maybe I shouldn't consider A
an Aggregate and work with B
separately and change the way I implemented my Repository/Usecase?
Or this check shouldn't even be considered a Business Rule? I know this is a more "abstract" question, but can't really see thought it.
Would really appreciate every help and clarification about the ways I could deal with this and if there's others mistakes I could have made since I'm still learning :)
bs
means b's as in plural. I wont mention what I thought it was before.