Representing collections in DDD and clean architecture.

226 views
Skip to first unread message

vivek poddar

unread,
Aug 1, 2016, 9:16:15 AM8/1/16
to Clean Code Discussion
Hi,

What is the advice on representing collection of objects in a system. Since we already we have abstractions like Array and Lists to represents collection it becomes difficult to make the choice.

For example I am reading the list of quotes in my repository:

public class QuoteRepository {
  class QuoteList extends ArrayList<Quote> {}

  public Quote[] all() {
    String path = "data/quotes.json";
    Gson gson = new Gson();
    try {
      BufferedReader br = new BufferedReader(new FileReader(path));
      return gson.fromJson(br, Quote[].class);
    } catch (IOException e) {
      e.printStackTrace();  
    }
    return new Quote[]{};
  }
}

is it ok to pass around the Array of quotes or should I wrap the response from the repository in a QuoteList abstraction?

Thanks,

Łukasz Duda

unread,
Aug 1, 2016, 10:37:43 AM8/1/16
to Clean Code Discussion
Hi,
I'd return List<Quote>. It's more abstract. ArrayList and array are more concrete, so it would be harder to change implementation.
You can find List<T> getEntities() in Java Case Study: https://github.com/cleancoders/CleanCodeCaseStudy/blob/281ad8546e6673c5631a8f8a3e2afed8af61c683/test/cleancoderscom/doubles/GatewayUtilities.java

Greetings

vivek poddar

unread,
Aug 11, 2016, 11:09:45 PM8/11/16
to Clean Code Discussion
Hi Lukasz,

What if I make a new collection type like QuoteDiscription or QuoteCollection and have apis on it like get, getByAuthor and getByCategory which would return me the respective random quote hiding the implementation details whether quotes are stored as HashSet ot LinkedList. wdyt?

Łukasz Duda

unread,
Aug 12, 2016, 4:12:54 AM8/12/16
to Clean Code Discussion
Hi,
I think it looks good, but I usually put such methods into gateway or repository. For me QuoteGateway or QuoteRepository would be an abstraction of persisted collection in this case.

vivek poddar

unread,
Aug 12, 2016, 5:03:35 AM8/12/16
to clean-code...@googlegroups.com
I think there is a greater confusion, what about the domain logic? I have listed some here https://github.com/aryehof/quote-service/blob/master/FEATURES.md

Following http://algs4.cs.princeton.edu/home/ the author told multiple times about abstract data collection.

I agree that I don't have the much knowledge about the Domain model but isn't this good to have a abstract collection to do business logic stuff and keep the repository dumb?

--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discussion+unsub...@googlegroups.com.
To post to this group, send email to clean-code-discussion@googlegroups.com.
Visit this group at https://groups.google.com/group/clean-code-discussion.

Reply all
Reply to author
Forward
0 new messages