Nested lists within a comment
felixbecker opened this issue · comments
Hi, first of all thank you very much fore sharing the code and producing such a great video. This is very valuable and I can imaging it helps a lot of developers including me.
I try to think a step further and it seams like I can not get my head around the following use-case. Basically how would I handle a list which will be accessed through the root. Bear with me while I try to come up with an example that might be related to comments-api
to explain it a little bit better.
How would I handle something like tagging a comment for example with cheers, likes, applause or emojis. Or tag it with labels as question, helpful etc.
Basically when I create a comment it contains the comment text like before. But I might have an empty list of tagables.
I can then assign a certain tag (whatever it might be) to a comment and also count them up if there are multiple of the same.
Would there be something like makeTagList and makeTag? Or would it be enough to just have the makeTag function injected into makeComment? Or how would I handle the nesting.
I hope may example makes sense and kind of transports what I am trying to ask.
It would be great, if you find the time to look at this and share your thoughts.
Looking forward to it.
Good question, so suppose we wanted to add the ability for users to add "reactions" to a comment (smileys, thumbs up, etc... like Github) and we also wanted to add the ability for users to "label" a comment (again like GitHub). Here's what we could do.
- Add 2 parameters to
makeComment
:labels
which is an array of stringsreactions
which has the shape:
{
[String.fromCodePoint(128077)]: ['user1@email.com','user2@email.com'],
[String.fromCodePoint(128515)] : ['user3@email.com']
}
- Add the following methods to our
comment
object:addReaction
which takes an emojiaddLabel
which takes a stringgetReactions
which returns our dictionary of reactionsgetLabels
which returns our array of labelsremoveReaction
which takes an emojiremoveLabel
which takes a string
In my first iteration of this code, I would enforce the business rules for reactions
and labels
directly inside the comment
object. If/when I discovered a case where I needed to use reactions
or labels
outside the confines of a comment
I would then create separate Entities for reactions
and labels
that would be invoked by the comment
object.
As a side note, Bob Martin's Clean Architecture calls all these things "Entities", but if you look at Domain Driven Design, you will see that a cluster of Objects that can be treated as a single unit is often called an "Aggregate". Here is a nice article about that: https://www.martinfowler.com/bliki/DDD_Aggregate.html