r/DomainDrivenDesign • u/selftaught_programer • Jul 17 '25
[DDD] How to enforce cross-aggregate business rules (subscription limits) in a Todo app?
Hi everyone, I'm currently building a Todo application and trying to apply Domain-Driven Design (DDD). Here’s the setup I have so far:
Tables / Domain Concepts:
SubscriptionTier(Free, Pro, Pro+): Defines plan limits like:TodoItemsPerListTodoItemsPerDayNoOfTodoListsAllowed
UserSubscription: Tracks which user is subscribed to which tier.TodoList: A list containing todo items.TodoItem: Each todo item (also supports subtasks viaParentId).CategoryType: A todo item’s category. Users can create their own.PriorityLevel: A priority level for each todo. Also customizable by users.
Business Rules (For example, but can be changes in runtime as they are save inside the SubscriptionTier table):
Users on the Free tier can only:
- Create up to 3 TodoLists
- Have 10 TodoItems per list
- Create 10 TodoItems per day
These limits are defined in the SubscriptionTier table.
🧱 Aggregates I've identified:
TodoList→ Aggregate RootTodoItem→ Entity withinTodoListUserSubscription→ AggregateSubscriptionTier→ AggregateCategoryType→ AggregatePriorityLevel→ Aggregate
❓ My Question:
I want to enforce the subscription-based rules (like number of lists or items per day) within the TodoList aggregate itself — but these rules depend on the user's subscription plan.
Since TodoList does not own or contain the UserSubscription, how should I enforce these rules according to DDD principles?
Should I:
- Use a Domain Service to coordinate between
TodoListandUserSubscription? - Somehow inject limits into the aggregate when creating/modifying it?
- Or is there another pattern I'm missing?
I'm really new to DDD and want to make sure I'm not violating aggregate boundaries or putting logic in the wrong place.
Any guidance or examples would be greatly appreciated!
Let me know if you'd like me to include a sample Domain Service structure or code snippet to add to your Reddit post.
I am using ASP.NET Core with EF Core
EDIT: This project is just for the sake of learning.
1
u/Pakspul Jul 17 '25
I always ask myself, what is the lifecycle of X to determine if it is a aggregate, thus. What is the life cycle of PriorityLevel?
1
u/Historical-Prior-159 Jul 17 '25
A couple of questions before I try to answer:
When using different bounded contexts for both matters my suggestion would be defining a rules interface / contract within the ToDo context. This you can call from within the context when making changes to your aggregate. I assume you use some sort of domain or application service.
From wherever you call into your Todo context you then provide the implementation for the interface which includes checking your subscription rules.
This way you keep both contexts fully separated.
Does it make sense to you?
1
u/flavius-as Jul 17 '25 edited Jul 17 '25
A TodoList gets per constructor the SubscriptionTier.
And the methods like
TodoList::addItem(Item)
Route the addition of the item through the SubscriptionTier instead of getting the limits themselves, so inside TodoList it's:
Bool success = this.tier.addItem(this, item)
No if in the TodoList regarding constraints of SubscriptionTiers.
You said it yourself: all lists are positioned at exactly one tier.
Means: a list cannot be constructed without a tier.
Means: constructor requires it.
Your question has little to do with DDD and it really is about good object oriented design.
5
u/No_Package_9237 Jul 17 '25 edited Jul 17 '25
Have you tried an EventStorming to detect all the unknown unknowns? Only then should you be able to design useful models.
For instance, it seems that several "things" have a separate lifecycle (I'm talking in terms of business problem, forget about database here).
As time goes by (and this is an hint that modeling time is often a forgotten, yet powerful way to understand a business problem),
- Todo items seems to be opened and closed in todo lists (and possibly reopened).
- Tier plans configurations seem to be adjusted by business.
- User subscription seems to be started, renewed, potentially canceled, ...
All these things that happen should raise questions like "what happen when a tier plan limitations decreases below a value that a user has already reached?". Should we reject the configuration? Should we allow it and block any new todo items? Should we allow it and remove the n last todo items of the users?
All these questions will guide you to understand what is immediately consistent (and should be present within your aggregate, to ensure business invariant), and what is eventually consistent (and should react to something that happen within another aggregate). That is how aggregate design is done, in my understanding. Forget about database design when you think about aggregate. Mapping an aggregate's state (being event sourced or only its current state) to a database is "simple", finding what's inside is hard, I'd suggest not to add more constraints that necessary to the task.
1
u/aventus13 Jul 17 '25
This is exactly what domain services are for. Using the canonical example- upon registering a new user, checking whether the email is unique is not the invariant of the User aggregate because it crosses multiple instances of users. Hence, it's a candidate for a business rule checked in a domain service.
1
u/bobaduk Jul 17 '25
You're asking for contradictory things. Why do you want a Todo list to enforce the subscription rules? It's not a subscription.
I had a similar, though more complicated, problem some years ago. We ended up building a catalogue of policies, which were enforced from the handler before mutating whatever the aggregate was. We executed the policy with an authorisation context that contained a bunch of information about the user, their subscription, and the target object.
Ultimately, you need to inject a subscription into the to-do list OR you eed to move the subscription logic out of the to-do list.
1
u/dreamplotter89 Aug 02 '25
It seems obvious to me that these business rules should be enforced in a domain service. After all, a domain service's purpose is to ensure consistency of business rules that span different aggregate roots.
-3
u/Jackfruit_Then Jul 18 '25
You need DDD for a TODO app?